computed 与 watch 区别 笔记
computed是可以监听到data中没有的 变量。举个栗子: 比如 type
type () {
return this.active == 'diagnosis' ? 'list' : 'before_list'
}
当 type的依赖 this.active 发生变化了 type可以监听到 ,同时 可以通过依赖的变化 将type的值改变。
watch是可以监听到props、data、 computed中(比如刚刚的type)的变化
如下图:
computed:{
updateData:() => store.updateData,
type () {
return this.active == 'diagnosis' ? 'list' : 'before_list'
}
},
watch:{
type(val, odlval){
console.log(val); //监听到变化了
},
watch还可以监听到具体变化前后的值val、oldval, 但是computed不行 ,只能监听到变化。
还没有评论,来说两句吧...