您应该传递一个对象而不是布尔值options
,因此:
mounted: function () { this.$watch('things', function () { console.log('a thing changed') }, {deep:true}) }
或者你可以vue
像这样将观察者设置到实例中:
new Vue({ ... watch: { things: { handler: function (val, oldVal) { console.log('a thing changed') }, deep: true } }, ... })
[试玩]
您应该传递一个对象而不是布尔值options
,因此:
mounted: function () { this.$watch('things', function () { console.log('a thing changed') }, {deep:true}) }
或者你可以vue
像这样将观察者设置到实例中:
new Vue({ ... watch: { things: { handler: function (val, oldVal) { console.log('a thing changed') }, deep: true } }, ... })
[试玩]
如果有人需要获取阵列中已更改的项目,请检查:
JSFiddle示例
帖子示例代码:
new Vue({ ... watch: { things: { handler: function (val, oldVal) { var vm = this; val.filter( function( p, idx ) { return Object.keys(p).some( function( prop ) { var diff = p[prop] !== vm.clonethings[idx][prop]; if(diff) { p.changed = true; } }) }); }, deep: true } }, ... })