-
computed
和watch
之间的区别与联系:-
computed
能完成的功能,watch
都可以完成,但一般来说computed
的写法会更简便。例如:姓名案例,
computed
、watch
都能完成,但computed
写起来,更简单。computed:{ fullName(){ return this.firstName + '-' + this.lastName } }
-
watch
能完成的功能,computed
不一定能完成。watch:{ firstName(value){ console.log('姓变了') this.fullName = value + '-' + this.lastName }, lastName:{ immediate:true, handler(value){ console.log('名变了') this.fullName = this.firstName + '-' + value } } }
-
-
理解
computed
(计算属性):-
侧重在【算】,核心是:计算出来的值。
-
靠
return
来输出计算的结果。 -
不能开启异步任务。
-
-
理解
watch
(监视属性):-
侧重在【监视】,核心是:
xxx
变了,我要做???
事。 -
无需
return
,靠内部逻辑去完成要做的事。 -
能开启异步任务。
-