【发布时间】:2019-06-18 05:44:59
【问题描述】:
我正在尝试从 customerProduct 数据对象计算总账单(所有产品*使用的总和)并显示账单金额。我正在调用计算方法来执行此操作。 customerProduct 数据是从created() 方法中的get api 调用中获取的。
问题:在初始渲染中,控制台显示以下错误:[Vue warn]: Error in render: "TypeError: Cannot read property 'PRICE' of undefined"。这是因为计算需要一些时间,同时当模板 html 代码呈现时,customerProductData 没有正确获取?
另外,可以在这里使用watch 属性帮助吗?
计算总费用的计算方法:
computed:{
billAmount(){
var billAmountWithoutDiscount = 0;
if(typeof(this.customerProductData[0].PRICE) == undefined){
return 0.0
}
else{
for(let i=0;i<this.customerProductData.length;i++){
billAmountWithoutDiscount += this.customerProductData[i].PRICE * this.customerProductData[i].USAGE;
}
return Number((100.0 - this.customerMetaData.discount)*billAmountWithoutDiscount/100).toLocaleString();
}
}
}
GET api 调用:
methods:{
async init(){
const response = await axios.get("/subscribe/getPresalesPricingMetaData/"+this.customerName)
this.customerProductData = response.data;
// console.log(response.data)
this.getCustomerMetaData();
},
}
customerProduct 对象:
customerProductData:[
0: {
'PRICE': 10,
'USAGE': 2000
},
1: {
'PRICE': 30,
'USAGE': 230
},
2: {
'PRICE': 50,
'USAGE': 200
},
3: {
'PRICE': 30,
'USAGE': 1000
},
]
折扣值:
customerMetaData:{
'discount': 2.2
}
【问题讨论】:
标签: vue.js