计算属性的本质:
computed:{
fullName:{
set: function(){ //设置值用的,但是一般我们只希望通过computed来计算值,并不修改值,所以set很少用到
this.firstName = "lao";
this.lastName = "wang";
},
get: function(){
return this.firstName + ' ' + this.lastName;
}
},
},
简写版:
computed:{
fullName(){ //去掉了set方法,只保留了get,相当于只读
return this.firstName + ' ' + this.lastName;
},
},
调用时: {{fullName}} 不用带上“()”
计算属性computed跟方法methods的区别:
计算属性是有缓存的,只有当它所依赖的值发生改变时它才会重新计算,否则每次使用时拿到的值都是之前缓存的结果,而普通的方法在多次使用时每次都会重复执行。