Vue從零開始總結(jié)9
剖析計算屬性的底層邏輯
computed:{
? ?fullName:function ()
? ?{
? ? ? ?return this.firstName+this.lastName;
? ?}
}
計算屬性一般不使用set方法,所以我們這么寫,純屬是簡化過來的
原寫法應(yīng)該是這樣的:
computed:{
fullName:{
get:function(){
return??this.firstName+this.lastName;
}
}
}
這兩個是等價的。
那我們要想寫的完整一些就是這樣
data:{
firstName:'a',
lastName:'b'
}
computed:{
fullName:{
set:function(x){
const name= x.split(' ');
this.firstName=name[0];
this.lastName=name[1];
}
get:function()
{
return??this.firstName+this.lastName;
}
}
}
標(biāo)簽: