【问题标题】:VueJS not setting properties after ajax call in mounted function在挂载函数中调用 ajax 后,VueJS 未设置属性
【发布时间】:2019-02-05 12:39:22
【问题描述】:

我正在尝试使用 VueJS,我有以下代码:

require('./bootstrap');

window.Vue = require('vue');

window.app = new Vue({
el: '#app',
data: {
    test: { }
},
methods: {
    getAttributes: function(){

        var pseudoThis = this;

        $.get('/api/getSpecifications').done(function(resp){
            pseudoThis.test = resp;
            console.log(resp);
        });

    }
},
mounted: function(){
    this.getAttributes();
}
});

ajax 调用被执行,我得到 json 结果。然而,无论我尝试什么,属性测试始终保持 {}。

有人能解释一下吗?

编辑:

做了更多的测试,还用我当前代码的副本做了一个小提琴: https://jsfiddle.net/cfhLg35v/2/

在小提琴中,一切都按预期工作,所以这似乎是一个本地问题。有人有想法吗?

【问题讨论】:

  • 请执行console.log(pseudoThis) 你可能会得到关于发生了什么的提示。

标签: jquery vue.js


【解决方案1】:

试试这个:

getAttributes () {
        $.get('/api/getSpecifications').done((resp) => {
            this.test = resp;
            console.log(resp);
        });

    }

【讨论】:

  • 相同的结果,属性没有被填充。
  • 我用我的代码副本做了一个小提琴,它确实有效。所以这似乎是一个本地问题。有什么想法吗? jsfiddle.net/cfhLg35v/2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
相关资源
最近更新 更多