我创建了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对象值? 如何更新'消息' ?
答案 0 :(得分:1)
您需要将正确的上下文绑定到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();
}
});