在寫項目的時候遇到了一個問題,就是須要動態監聽data中一個對象的屬性的變化。遇到了許多坑,在此過程當中也發現了兩種解決方案。javascript
data() { return { parent:{ child:1 } }; },
watch:{ 'parent.child':{ deep:true, handler: function(newV, oldV) { console.log(newV); } } }
2、經過computed作中介html
computed:{ newChild(){ return this.parent.child; } }
watch:{ newChild(newV,oldV){ alert(newV) } },