Vue 的 computed属性计算购物车总价
productList是要计算的商品的数据
productList: [{
id: 1,
title: "商品标题",
desc: "商品描述",
price: "66",
tag1: "66积分",
tag2: '积分购买',
thumb: "https://img.yzcdn.cn/vant/ipad.jpeg",
num: 1,
checked:''
},
{
id: 2,
title: "商品标题",
desc: "商品描述",
price: "99",
tag1: "99积分",
tag2: '积分购买',
thumb: "https://img.yzcdn.cn/vant/ipad.jpeg",
num: 1,
checked:''
}]
computed:{
sumPrice(){
//用一个变量准备接住计算后的值
let sumPrice=0
//遍历productList,拿到单价和数量
for(let i in this.productList){
//总价为每个商品的价格(price)*数量(num) 我这里*100是因为组件的单位
sumPrice+=(this.productList[i].price*this.productList[i].num*100)
}
//最后return出去计算好的值,直接用就好了,不用在data中起总价的变量,不然会报错
return sumPrice
}
}