computed计算,针对多个数据,影响一个数据,
如以下的,price或者num变化时,从而计算总价total
computed: {
//total根据price、num的变化而变化
total: function () {
return this.price * this.num;
}
}
watch 一个数据影响多个数据
- 如果监听的是引用对象,则需要 使用JSON对象, deep: true,handler: function 用来处理逻辑----深度监听
- 如果是基本数据类型,则可以使用方法 ,第一个参数是 修改之后的数据, 第二个参数是修改之前的数据
watch: {
//监听一个变量时,可直接监听
username: function (newValue, oldValue) {
console.log('newValue : ' + newValue);
console.log('oldValue : ' + oldValue);
this.tips = '我改变了';
this.tips1 = '我改变了';
this.tips2 = '我改变了';
debugger
}
}
watch: {
//当监听的studentsArr是一个对象时,需要深度监听
studentsArr: {
handler: function (newValue) {
console.log('newValue : ' + newValue);
console.log('oldValue : ' + oldValue);
},
deep: true
},
}