我创建 Vue 对象并需要在收到响应时更新一些数据
new Vue({
el: "#app",
data: {
message: [1,2,3,]
},
methods: {
getList: function () {
var myGet = axios.get('/api/guests/.json')
.then(function (response) {
// ????????? message = response.data;
})
.catch(function (error) {
console.log(error);
});
}
},
created:function () {
this.getList();
}
});
如何更新此对象内的 vue 对象值? 如何更新“消息”?
最佳答案
您需要将正确的上下文绑定(bind)到 then
回调并访问 this.message
。你必须做这样的事情
new Vue({
el: "#app",
data: {
message: [1,2,3,]
},
methods: {
getList: function () {
var myGet = axios.get('/api/guests/.json')
.then(function (response) {
this.message = response.data;
}.bind(this))
.catch(function (error) {
console.log(error);
});
}
},
created:function () {
this.getList();
}
});
关于javascript - 如何更新此对象内的 vue 对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44405502/