【发布时间】:2019-10-27 05:23:23
【问题描述】:
问题
我有一个包含多个对象的“产品”数组。每个产品对象都包含属性“价格”。我想在每个产品中查看此属性以了解可能的更改。当用户在输入框中更改价格时,我使用它来计算佣金价格。
我的产品数组如下所示;
[
0: {
name: ...,
price: ...,
commission: ...,
},
1: {
name: ...,
price: ...,
commission: ...,
},
2: {
name: ...,
price: ...,
commission: ...,
},
...
...
...
]
我的代码
我试过这个,但除了产品首次加载时,它没有检测到任何变化;
watch : {
// Watch for changes in the product price, in order to calculate final price with commission
'products.price': {
handler: function (after, before) {
console.log('The price changed!');
},
deep : true
}
},
产品是这样加载的;
mounted: async function () {
this.products = await this.apiRequest('event/1/products').then(function (products) {
// Attach reactive properties 'delete' & 'chosen' to all products so these can be toggled in real time
for (let product of products) {
console.log(product.absorb);
Vue.set(product, 'delete', false);
Vue.set(product, 'chosen', product.absorb);
}
console.log(products);
return products;
})
}
我看过的其他问题 Vue.js watching deep properties 这个试图观察一个尚不存在的属性。 VueJs watching deep changes in object 这个正在监视另一个组件的变化。
【问题讨论】:
标签: javascript vue.js vuejs2 watch