[英]Vue2 data not being updated in template after retrieving from url
I am using Vue.js version 2, trying to update a variable which is used in the view. 我正在使用Vue.js版本2,尝试更新视图中使用的变量。
Bear with me, I have the following code: 忍受我,我有以下代码:
new Vue({
el: '#products',
data: {
showProductModal: false,
product: {}
},
methods: {
retrieveProduct(id) {
axios.get('/product/'+id).then(function (response) {
this.product = response.data;
console.log(this.product);
});
}
}
Please note that: 请注意:
The console.log(this.product) results in console.log(this.product)导致
{id: 1, name: "Lemon cake", description: "Traditional lemon cake", pic_url: "", category_id: 1, …} {id:1,名称:“柠檬蛋糕”,描述:“传统柠檬蛋糕”,pic_url:“”,category_id:1,...}
which is exactly the expected output. 这正是预期的产出。 However, upon calling {{ product }} in the view, the template is not updated in any way, and gets stuck forever in {}. 但是,在视图中调用{{product}}后,模板不会以任何方式更新,并永远停留在{}中。
I have also tested if two-way databinding is working, and the answer is yes. 我还测试了双向数据绑定是否有效,答案是肯定的。 Actually, everything is working perfectly apart from this. 实际上,一切都与此完全不同。 Am I doing something wrong? 难道我做错了什么? Should I expect that the template is not updated after retrieving an object from a promise? 从promise中检索对象后,我是否应该期望模板未更新?
Any help is appreciated. 任何帮助表示赞赏。 Thank you in advance. 先感谢您。
EDIT: 编辑:
You could be running into issues with self
, whether due to lack of declaration or scoping issues. 无论是由于缺乏声明还是范围问题,您都可能遇到self
问题。 Try the following instead: 请尝试以下方法:
retrieveProduct(id) {
var this_vue_instance = this;
axios.get('/product/'+id).then(function (response) {
this_vue_instance.product = response.data;
console.log(this_vue_instance.product);
});
}
I think it should be some issues with scoping. 我认为这应该是范围界定的一些问题。 Try this: 尝试这个:
retrieveProduct(id) {
axios.get('/product/' + id).then(response => {
this.product = response.data;
});
}
And then use vue-devtools
to check if the data is properly changed. 然后使用vue-devtools
检查数据是否正确更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.