1 回答

TA貢獻(xiàn)1946條經(jīng)驗 獲得超3個贊
為了防止總數(shù)在以后的通話中增加一倍,只需添加一個局部變量即可將您的數(shù)量和價格相加。完成價格和數(shù)量的累加后,您可以為它們分配實際的類別字段,請參見下面的示例:
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;
})
}
添加回答
舉報