简体   繁体   English

Vue.js 数据绑定问题

[英]Vue.js Data binding issue

I am trying to bind data in my userDetails array through post request but i did not get the data , i had tried all the possible solution given in stackoverflow and also in vue.js docs but nothing helps me here is my code.我正在尝试通过发布请求在我的 userDetails 数组中绑定数据,但我没有得到数据,我已经尝试了 stackoverflow 和 vue.js 文档中给出的所有可能的解决方案,但我的代码对我没有任何帮助。

ND.user_list = new Vue({
el: '#user_list',
data: {
  userDetails: []
},
mounted: function () {
  $.post(ND.routes['users.get'], [], function(data) {
    //console,log(data);
    this.$set(this, 'userDetails', data);
    //this.usersDetails = data;

  }.bind(this), 'json');
  //onsole.log(this.userDetailsdata);
}


});

i am getting data in console log in mounted function but not getting in userDetails array declare in data.我在安装函数的控制台登录中获取数据,但没有进入数据中声明的 userDetails 数组。 here is my view code这是我的查看代码

<div class="ui grid container" id="user_list">

  <table class="ui celled padded table">
    <thead>
      <tr>
        <th colspan="6">Top Users</th>
      </tr>
    </thead>
      <thead>
      <tr>
        <th class="ui center aligned">#</th>
        <th class="ui center aligned">Name </th>
        <th class="ui center aligned">Email </th>
        <th class="ui center aligned">User Type</th>
        <th class="ui center aligned">Action </th>
      </tr>
    </thead>
    <tbody>
      {% verbatim %}
    <tr v-for="user in userDetails">
      <td>1</td>
      <td class="single line">{{ user.email }}</td>
      <td class="ui center aligned "></td>
      <td class="ui center aligned "></td>
      <td class="ui center aligned ">
      <select class="ui dropdown">
        <option value=" ">Action </option>
        <option value="1">Edit</option>
        <option value="0">Delete</option>
      </select>
      </td>
    </tr>
          {% endverbatim %}


    </tbody>
  </table>
</div>  

Thanks in advance.提前致谢。

The this in the callback fo $.post() does not refer to the VueJS instance. $.post()回调中的this不引用 VueJS 实例。 This means that this.userDetails actually will return undefined within the callback.这意味着this.userDetails实际上会在回调中返回 undefined 。 What you can do is to create a proxy for the VueJS instance as such:您可以做的是为 VueJS 实例创建一个代理,如下所示:

ND.user_list = new Vue({
  el: '#user_list',
  data: {
    userDetails: []
  },
  mounted: function () {
    // Proxy `this`
    var that = this;

    $.post(ND.routes['users.get'], [], function(data) {

      // `that` will refer to VueJS instance
      that.userDetails = data;

    }, 'json');
  }
});

You are using this.$set wrong: it should only be used when you got an object in the data object and want to add properties to that object that wasn't there before, or if you want to change indexes of the array.您使用 this.$set 错误:仅当您在数据对象中有一个对象并希望向该对象添加以前不存在的属性时,或者如果您想更改数组的索引时,才应使用它。 Then first argument should not be the vue object, but the data object (ie the object or array in the data object).那么第一个参数不应该是vue对象,而是数据对象(即数据对象中的对象或数组)。 See documentation here: https://v2.vuejs.org/v2/guide/reactivity.html or https://v2.vuejs.org/v2/api/#Vue-set .请参阅此处的文档: https ://v2.vuejs.org/v2/guide/reactivity.html 或https://v2.vuejs.org/v2/api/#Vue-set

Instead of: this.$set(this, 'userDetails', data);而不是: this.$set(this, 'userDetails', data); you should just do this.userDetails = data;你应该这样做this.userDetails = data; . . Have you tried this?你试过这个吗? Also, if you change elements in the array, then you should use Vue.set or splice, but read the documentation carefully before you use it.另外,如果你改变了数组中的元素,那么你应该使用 Vue.set 或 splice,但在使用之前请仔细阅读文档。

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

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