javascript - 每次添加产品时增加 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);
});
}
解决方案
totalAmount: number=0;
this.totalAmount += (item.qtn * item.price)
推荐阅读
- email - 使用 PAC 文件绕过 SMTP/IMAP 代理流量
- java - 使用 JDK HttpClient 下载大文件时出现 OutOfMemoryError
- html - 无法使用 Python 表单登录
- javascript - this.userService.info$.subscribe() 方法在 ngoninit 中不起作用
- python - 如何使用 office365 在 Django 中发送电子邮件
- amazon-web-services - 能否将 AWS Rekognition 自定义标签模型部署到另一个账户?
- drake - 您如何在 Drake 可视化器上可视化您的 ROS URDF?
- jenkins - 如何在 Jenkins 中正确读取属性文件?
- linux - 将 /dev 挂载到 Docker 容器中是否安全
- identityserver4 - 如何在 foxx 框架中验证 Oauth 访问令牌