简单通俗的理解:
computed: 别的一个或者多个属性改变,(结果)导致这个值发生变化
watch :监听的特定值的改变(起因),导致做一些动作或者改变别的值
应用场景:
今天碰到的场景,把一个浮点数例如103.56,拆成5个整数1,0,3,5,6,每个整数用滑轮组件改变它的值,再组合起来成为新的浮点数999.37
1.有5个整数,每个整数改变都要计算一下形成新的浮点数
用watch的话,
watch: {
num1:function(){
.....
},
num2:function(){
},
...
num5:function(){
}
}
监听5个属性,而且内容都是一样实在是令人难以接受
computed: {
fudian: {
get () {
return (this.num1 * 100 + this.num2*10 + this.num3 + this.num4 * 0.1 + this.num5 * 0.01).toFixed(2)
},
set () {
this.fudianshu = this.num1 * 100 + this.num2*10 + this.num3 + this.num4 * 0.1 + this.num5 * 0.01).toFixed(2)
}
}
不管这个式子里边哪个改变了,都会改变这个值