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