Vue为什么不能立即检测变量值的更改?

huangapple go评论60阅读模式
英文:

Why doesn't Vue detect the variable value change immediately?

问题

Here is the translated code portion:

我有一个从后端获取数据的方法,并将我的数据变量设置为从后端获取的数据。我在onMounted vue方法中调用这个方法。但是当我在组件中显示数据时,它是空的,长度为0。奇怪的是,如果我对组件进行任何更改,数据就会正确显示,并且长度会更改为正确的长度。

这是我的代码:

<script setup>
import SearchBar from '@/Components/Admin/SearchBar.vue';

import { onMounted, reactive } from 'vue';

import moment from 'moment';

let data = [];

onMounted(() => {

   getBreeders();

})

const getBreeders = () => {

   axios.get(route('tagsmodal.breeders')).then(response => {

       data = response.data;
       console.log(data);

   })
}

</script>

和组件:

{{ 'the length is '+data.length }}

<div class="table-view" id="entityTableView">
<table class="fullwidth">
   <thead>
      <tr>
           <th scope="col" data-attribute="title" class="orderable">Title</th>
           <th scope="col" data-attribute="postDate" class="ordered asc">Post Date</th>
       </tr>
   </thead>
   <tbody id="Tbody" >
       <tr v-for="(entity,index) in data" :key="entity.id" >
           <td> {{entity.name}} </td>
           <td> {{ moment(String( entity.created_at)).format('YYYY-MM-DD') }} </td>
       </tr>
   </tbody>                             
</table>

Please note that I have only translated the code portion and not provided answers to the translation request.

英文:

I have a method that gets data from the backend, and set my data variable to the data that's coming from the backend. I call the method inside the onMounted vue method. But when I display the data in my component it's empty and the length is 0. Strange enough, if I made any changes to the component the data displays correctly and the length changes to the correct length.

here is my code:

&lt;script setup&gt;
 import SearchBar from &#39;@/Components/Admin/SearchBar.vue&#39;;

 import {onMounted, reactive} from &#39;vue&#39;

 import moment from &#39;moment&#39;

 let data = [];
 
 onMounted(() =&gt; {

    getBreeders();

})

 const getBreeders = () =&gt; {

    axios.get(route(&#39;tagsmodal.breeders&#39;)).then(response=&gt;{

        data = response.data;
        console.log(data);

    })
 }

</script>

and the component:

{{ &#39;the length is &#39;+data.length  }}

&lt;div class=&quot;table-view&quot; id=&quot;entityTableView&quot;&gt;
&lt;table class=&quot; fullwidth&quot;&gt;
    &lt;thead&gt;
       &lt;tr&gt;
            &lt;th scope=&quot;col&quot; data-attribute=&quot;title&quot; class=&quot;orderable&quot;&gt;Title&lt;/th&gt;
            &lt;th scope=&quot;col&quot; data-attribute=&quot;postDate&quot; class=&quot;ordered asc&quot;&gt;Post Date&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody id=&quot;Tbody&quot; &gt;
        &lt;tr v-for=&quot;(entity,index) in data&quot; :key=&quot;entity.id&quot; &gt;
            &lt;td&gt; {{entity.name}} &lt;/td&gt;
            &lt;td&gt; {{ moment(String( entity.created_at)).format(&#39;YYYY-MM-DD&#39;) }} &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;                             

</table>

the data length is always zero, and no data is displayed until I made some change to the component

答案1

得分: 2

你需要将 data 转换为一个 ref,这样 Vue 才能检测到它的变化:

import {ref} from 'vue'
const data = ref([])

在你的 getBreeders() 方法中,然后将获取的数据赋值给 ref 的值:

const getBreeders = async () => {
    const response = await axios.get(route('tagsmodal.breeders'))
    data.value = response.data;
}

然后就是这样。

英文:

You have to turn data into a ref, so that Vue can detect changes to it:

import {ref} from &#39;vue&#39;
const data = ref([])

In your getBreeders() method, you then assign the fetched data to the ref's value:

const getBreeders = async() =&gt; {
    const response = await axios.get(route(&#39;tagsmodal.breeders&#39;))
    data.value = response.data;
}

And that should be all

huangapple
  • 本文由 发表于 2023年4月17日 01:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定