computed和lwatch的区别:
1,computed能完成的功能,watch都可以完成。
2,watch能完成的功能,computed不一定能完成,例如: watch可以进行异步操作。两个重要的小原则:
<1>所被Vue管理的函数,最好写成普通函数,这样this的指向才是vm或组件实例对象.
<2>所有不被Vue所管理的函数(定时器的回调函数、ajax的回调函数等、Promise的回调函数),最好写成箭头函数,这样this的指向才是vm或组件实例对象。
3,watch擅长处理的场景:一个数据影响多个数据
computed擅长处理的场景:一个数据受多个数据影响
4,computed是计算属性,也就是依赖某个值或者props通过计算得来得数据;例如:
computed:{
comFunc:{
get(){
console.log("触发了computed的get方法");
return this.name+"-----"+this.userName
},
set(value){
console.log("set"+value);
this.name = "李";
this.userName = "四";
}
},
而watch是监听器,可以监听某一个数据,然后执行相应的操作;例如:
watch: {
inputValue:{
handler(newName, oldName){
this.watchInputValue = newName
}
}
}