计算属性computed
计算值 offerTotal 当里面使用的值发生变化时 offerTotal也会发生变化,页面视图也会发生变化
computed: {
//不需要在data里注册
offerTotal: function() {
let prices = 0;
this.myproduct.lists.forEach(ele => {
prices += Number(ele.demandPrice);
});
return prices;
},
}
使用计算值 offerTotal
//html里使用
<p>{{offerTotal}}</p>
//在js中使用
this.offerTotal
js操作,有小数时显示 两位小数
let prices = 1.0001 || 1000
if (String(prices).indexOf(".") + 1 > 0) { //当包含"."
console.log(prices);
prices = prices.toFixed(2);
}