1.普通变量(number,string...)
watch:{
data:function(){ //注意不用箭头函数,理由是箭头函数绑定了父级作用域的上下文,所以 this 将不会按照期望指向 Vue 实例
console.log("改变");
}
},
2.数组
数组改变时建议用原生操作数据的方式进行改变,比如push之类。
return array=[1,2,3];
array2:{ handler(newValue, oldValue) { console.log(newValue) }, deep: true },
3.对象
return obj1 = {
label:1,
value:2
}
obj1.label=2;
watch:{
obj1:{
handler(newValue, oldValue) {
console.log(newValue)
},
deep:true //深拷贝
}
}
//如果要检测某一个特定属性的改变,可以用computed
computed: { value() { return this.obj1.value } },
watch:{
value(newValue, oldValue) { console.log(newValue,oldValue) },
网友评论