vue事件监听
事件监听:watch
data: {
a:0,
b:0,
c:0
},
methods:{...},
watch:{
//监听属性变化前后
a:function(newA,oldA){
this.c=newA+this.b
console.log(oldA);
},
//简化
b(newB){
this.c=this.a+newB
}
}
同时监听多个属性
computed:{
changeAB(){
const {a,b}=this
return{a,b}
}
},
watch:{
//通过监听计算属性 监听数据变化
changeAB(newObj){
console.log(newObj);
this.c=newObj.a+newObj.b
}
}
微信小程序中使用 observers 事件监听
observers:{
//同时监听多个属性 'a,b,c'
'a,b':function(newA,newB){
this.setData({
c: newA+newB
})
}
}