Angular4 reduce 价格累加报错

麻烦帮忙看一下

报错信息:

谷歌Chrome:

Error TypeError: Cannot read property 'reduce' of  undefined
at checkAndUpdateView(core.es5.js:12241)


火狐Firefox:

TypeError: this.shoppingcartList is undefined
MallShoppingCartComponent.prototype.getTotal@http://localhost:4200/0.chunk.js:1408:9

============================

代码片段(shopingcartList 就是返回的数据列表):

public shoppingcarList: any;

ngOnInit(){
this.getMockTest();
}

getMockTest(){
this.ishoppingcarApi.getLIstUsingGET().subscribe(
  result => {
    this.shopingcartList =result;
    ... ...
}


getTotal(){        // 价格累加
return this.shoppingcartList.reduce((state, order) =>{
 return order.cartCommodities.reduce((state,courseList => {
return state + (courseList.checked ? courseList.price ? courseList.price.presentPrice : 0);
}, state);

}, 0);
}


html 模板中
{{getTotal()}}
阅读 2k
1 个回答

shoppingcartList未定义,你没有赋值

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进