vue.js $watch 对象数组 [英] vue.js $watch array of objects
本文介绍了vue.js $watch 对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
mounted: function() {
this.$watch('things', function(){console.log('a thing changed')}, true);
}
things
是一个对象数组 [{foo:1}, {foo:2}]
$watch
检测何时添加或删除对象,但不会检测何时更改对象上的值.我该怎么做?
$watch
detects when an object is added or removed, but not when values on an object are changed. How can I do that?
推荐答案
你应该传递一个对象而不是布尔值作为 options
,所以:
You should pass an object instead of boolean as options
, so:
mounted: function () {
this.$watch('things', function () {
console.log('a thing changed')
}, {deep:true})
}
或者你可以像这样将观察者设置到 vue
实例中:
Or you could set the watcher into the vue
instance like this:
new Vue({
...
watch: {
things: {
handler: function (val, oldVal) {
console.log('a thing changed')
},
deep: true
}
},
...
})
这篇关于vue.js $watch 对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文