简体   繁体   English

为什么我的 VUE 数组元素没有渲染,

[英]Why my VUE array elements are not rendered,

I can get the elements sent from the backend.But the text can't display.I don't know why.我可以获取从后端发送的元素。但是文本无法显示。我不知道为什么。 here is my html:这是我的 html:

              <!-- <form action=""> -->
                <div class=" form-group">
                  <input v-model="wd" @keyup="keyup($event)"  type="text" class="form-control" />

                  <!-- <ul class="list-group"> -->
                   
                    <table>


                      <tr>
                        <th v-for="item in title"><div style="width:500px">{{item}}</div> </th>
                      </tr>
                      <tr v-for="item in arr" :key='item'>
                        <td>{{item.name}}</td>
                        <td>{{item.sort}}</td>
                        <td>{{item.company}}</td>
                      </tr>
                    </table>
                 
                </div>

here is My Vue:这是我的 Vue:

        el: "#app",
        data: {
          wd: '', 
          arr: [], 
          listIndex: -1, 
          title:['name','sort','company'],
        },
        methods: {
          
          keyup(event) {

            var url = "/search/search/"
            axios.get(url, {
              params: {
                q: this.wd,
              }
            }).then(res => {
              console.log(res);
              
              this.arr = res.data.list; 
              
            
             
            })
            
          }

        }
      })
      ;

I know the data was got.Because the number of the items in loop is exactly correct.When I console.log(res.data.list),I can view the data from the backend.But they can't display in my page.我知道数据得到了。因为循环中的项目数完全正确。当我控制台.log(res.data.list)时,我可以从后端查看数据。但它们无法显示在我的页面中. I will be very appreciate it if you could help me.Thank you!如果您能帮助我,我将不胜感激。谢谢! enter image description here在此处输入图像描述

I think the problem is in your reference to this.arr in your callback.我认为问题在于您在回调中对this.arr的引用。 When the AJAX call returns, this is scoped to the response, not your Vue object.当 AJAX 调用返回时, this仅限于响应,而不是您的 Vue object。

You can overcome this by inserting a variable prior to the AJAX call, like this:您可以通过在 AJAX 调用之前插入一个变量来克服这个问题,如下所示:

var vm = this;
axios.get(url, {

then, in the callback, instead of this.arr = res.data.list;然后,在回调中,而不是this.arr = res.data.list; , do this: , 做这个:

vm.arr = res.data.list;

The field data must be reactive字段data必须是反应性的

data() {
  return{
   wd: '', 
   arr: [], 
   listIndex: -1, 
   title:['name','sort','company'],
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM