ready: function() {
this.$watch('things', function(){console.log('a thing changed')}, true);
}
things
の配列を見るオブジェクト[{foo:1}, {foo:2}]
$watch
のアレイは、オブジェクト上の値が変更されたときにオブジェクトを追加または削除ではなく、場合に検出されます。どうやってやるの?
ready: function() {
this.$watch('things', function(){console.log('a thing changed')}, true);
}
things
の配列を見るオブジェクト[{foo:1}, {foo:2}]
$watch
のアレイは、オブジェクト上の値が変更されたときにオブジェクトを追加または削除ではなく、場合に検出されます。どうやってやるの?
あなたはそう、options
として代わりにブールのオブジェクトを渡す必要があります。
ready: 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
}
},
...
})
をアレイ内で変更されたアイテムを取得する必要がある場合は、それを確認してください:
ポストのコード例:
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
}
},
...
})
ニース、これは移行の問題でした。 vueの最後のバージョンでは、最後の引数が 'deep'を意味するため、' true'を使用しました。タイ! –
私のために働いた。ありがとう! – Lorenzo
'ready'はvue 2.0で' mounted'に変更されました! – user1506145