computed是计算属性,支持缓存,可以有多个依赖,如果是在缓存数据的基础上更新,用计算属性
watch是侦听器,不支持缓存,只能有一个依赖
methods是方法,只要重新渲染就会执行,如需要每次重新计算,用方法
computed和watch
computed可以有多个依赖,基于它的依赖进行缓存,以下方法中firstName和lastName是这个属性的两个依赖,当任意变量发生变化时 fullName都会重新计算。
data:{
firstName:"ying",
lastName:"wang",
fullName: ""
},
computed:{
fullName:function(){
return this.firstName + " " +this.lastName;
}
}
watch只能有一个依赖
watch: {
firstName: function (val) {
this.fullName = val + ' ' + this.lastName
},
lastName: function (val) {
this.fullName = this.firstName + ' ' + val
}
}