1 回答
TA贡献1946条经验 获得超3个赞
为了防止总数在以后的通话中增加一倍,只需添加一个局部变量即可将您的数量和价格相加。完成价格和数量的累加后,您可以为它们分配实际的类别字段,请参见下面的示例:
totalCart$: Observable<ShoppingCartItem[]>;
items: ShoppingCartItem[]
totalQuantity: number = 0;
totalPrice: number = 0;
constructor(
public dialog: MatDialog,
private shoppingCartService: ShoppingCartService,
) { }
ngOnInit() {
this.getCart();
}
async getCart() {
this.totalCart$ = await this.shoppingCartService.getCart();
this.totalCart$.subscribe(data => {
let totalQuantity = 0;
let totalPrice = 0;
data.forEach(element => {
totalQuantity += element.quantity
totalPrice += element.quantity * element.price
console.log(totalQuantity, totalPrice);
})
this.totalQuantity = totalQuantity;
this.totalPrice = totalPrice;
})
}
添加回答
举报