首页 > 解决方案 > 每次添加产品时增加 totalAmount

问题描述

添加产品时出现问题,totalAmount 显示为 20 我不知道为什么?如果我增加 totalAmount 不动,我该怎么做?

谢谢

ts.文件

     productList = [
    { id: 1, name: 'Louis Vuis', price: 10, qtn: 1 },
    { id: 2, name: 'shubert helmet', price: 20, qtn: 1 },
  ];

  productArray: any = [];

  totalAmount: number;

  ngOnInit() {
    this.totalPrice();
  }

  add(product, idx) {
    const found = this.productArray.find(
      item => JSON.stringify(item) === JSON.stringify(product)
    );
    if (found) {
      this.productArray[idx].qtn++;
    } else {
      this.productArray.push(product);
    }
  }

  totalPrice() {
    this.productList.forEach(item => {
      this.totalAmount = (item.qtn * item.price)
      console.log(this.totalAmount);
    });
  }

在此处输入图像描述

标签: javascriptangulartypescriptcart

解决方案


totalAmount: number=0;
this.totalAmount += (item.qtn * item.price) 

推荐阅读