vue中提供在watch监听时设置deep:true 实现对对象的深度监听
直接监听对象
watch:{
obj:{ //监听的对象
deep:true, //深度监听设置为 true
handler:function(newV,oldV){
console.log('watch中:',newV)
}
}
}
监听对象下的其中一个属性
data () {
return {
obj:{
name:'老蒋坏得很',
age:18
}
}
},
watch:{
'obj.name':{
deep:true,
handler:function(newV,oldV){
console.log('watch中:',newV)
}
}
}
computed配合watch实现单个属性的深度监听
data () {
return {
obj:{
name:'老蒋坏得很',
age:18
}
}
},
computed:{
name(){
return this.obj.name;
}
},
watch:{
name(newval,oldval){
console.log('新:',newval);
console.log('旧:',oldval)
}
}