2017-07-11 16 views
0

のは私がsecond要素のbarプロパティを変更するとデータセクションVueJSで監視対象配列内のどの項目が変更されたかを知るにはどうすればよいですか?

data() { 
    return { 
     myData : [{foo:2, bar:3},{foo:4,bar:5}] 
    } 
} 

で宣言された配列があるとしましょう、私は2番目の要素が変更されたことを知ってほしいです。

時計の機能はどのように見えますか?

watch : { 
    myData : { 
    deep : true, 
    handler(oldVal, newVal) { 
     console.log("New val is the entire myData[] array which is not what I want", newVal); 
     // I also want to know what index of the array the changed value belonged to. 

    } 
    } 
} 

答えて

-1

まず、あなたの引数が、それはまたnewVal, oldVal

する必要がありますように、私はこの警告は、ここで適用される場合があります考えて、ウォッチャーに後方です: https://vuejs.org/v2/api/#vm-watch

Note: when mutating (rather than replacing) an Object or an Array, the old value will be the same as new value because they reference the same Object/Array. Vue doesn’t keep a copy of the pre-mutate value. 
+0

それでも、私は入手できますか変更されている配列オブジェクトのインデックス? –

+0

私はおそらくあなたが最初に配列を突然変異させる方法に依存すると思います – aprouja1

関連する問題