第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有一種方法可以合并單據(jù)值(購物車的總價格和數(shù)量)

有沒有一種方法可以合并單據(jù)值(購物車的總價格和數(shù)量)

胡子哥哥 2021-04-08 13:15:47
如標(biāo)題所述,在顯示價格和數(shù)量方面存在問題。這是我到目前為止所做的:購物車項目 export class ShoppingCartItem {  id?: string;  name: string;  imgCover: string;  price: number;  quantity: number;}購物車服務(wù)async getCart(): Promise<Observable<ShoppingCartItem[]>> {  let cartId =  await this.getOrCreateCartId();  let cartCollection = this.afs.collection(`shopping-carts/${cartId}/items`, ref => ref.where('id','==', cartId));  return cartCollection.snapshotChanges().pipe(map( actions => {    return actions.map(a => {      const data = a.payload.doc.data() as ShoppingCartItem;      const id = a.payload.doc.id;      return { id, ...data };    });  }));}navbar.component  totalCart$: Observable<ShoppingCartItem[]>;  items: ShoppingCartItem[]  total: number = 0;  constructor(   public dialog: MatDialog,   private shoppingCartService: ShoppingCartService,  ) {}  ngOnInit() {    this.getCart();  }  async getCart() {    this.totalCart$ = await this.shoppingCartService.getCart();    this.totalCart$.subscribe(data => {      data.forEach(element => {       this.total += element.quantity       console.log(this.total);   })  }) }使用這種方法,我可以在第一次加載時顯示正確的數(shù)據(jù),然后將該數(shù)量乘以相同數(shù)量+ 1(因為再次調(diào)用現(xiàn)有數(shù)據(jù))就可以顯示正確的數(shù)據(jù)。如何合并數(shù)量和價格字段?
查看完整描述

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;

  })

}


查看完整回答
反對 回復(fù) 2021-04-29
  • 1 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號