挂载在vue实例上 计算对象,只要依赖的值有变化就会执行,计算属性写成函数,默认调get方法,但是它不支持异步。
computed{
checkAll(){
get(){
return this.products.every(p=>p.isSelected)
},
set(val){
this.products.forEach(p=>p,isSelected =val)
}
},
sum:{//sum的结果会被缓存 如果依赖的数据没有变化就不会重新执行
get(){
return this.products.reduce(prev,next)=>{
if(!next.isSelected)return prev;
return prev+next.productPrice*next.productCount
},0);
}
}
**等价于**
sum(){
return this.products.reduce(prev,next)=>{
if(!next.isSelected)return prev;
return prev+next.productPrice*next.productCount
},0);
}
}