普通数据类型可以使用watch直接监视,复杂数据类型Object Array要使用深度监视
复杂数据类型需要使用deep属性(深度监听),和handler方法。
复杂数据类型Object Array要使用深度监视
new Vue({
el:'#app',
data: {
msg:'',
stu:[{name:"jack"}]
},
watch:{
普通数据类型
'msg':function(newV,oldV){
if(newV==='100'){
console.log("jfsf");
}
},
//使用深度监视 object Array,普通监视只会监视到对象和数组的地址
"stu":{
deep:'true',
handler:function(newV,oldV){
console.log(newV[0].name)
}
}
}
});
Vue不能够检测到对象属性的添加或者删除,因为在Vue初始化实例的时候,才会通过defineProperty()的getter/setter劫持data对象上的属性,从而监听属性的变化,实现响应式。