javascript - 错误类型错误:当我尝试添加产品时无法读取未定义的属性“名称”
问题描述
我尝试尽可能简单地制作一个篮子,我尝试将产品添加到我的篮子中,但我有一个错误,但我找不到我的错误。
谢谢你
html
<h2>Product</h2>
<div class="card" *ngFor="let product of productList">
<h1>{{product.name}}</h1>
<p class="price">{{product.price | currency: 'USD'}}</p>
<p><button (click)=add()>Add to Cart</button></p>
</div>
</div>
<div>
<h2>Total</h2>
<div class="card">
<table>
<thead>
<tr>
<th>product</th>
<th>price</th>
<th>Quantity</th>
<th>total</th>
</tr>
</thead>
<tbody *ngFor="let added of productArray">
<tr>
<td>{{added.name}}</td>
<td>{{added.price}}</td>
<td>x 10</td>
<td>45€</td>
ts.文件
productList = [
{ name: 'Louis Vuis', price: 10 },
{ name: 'shubert helmet', price: 20 },
{ name: 'sport gloves', price: 30 }
];
productArray: any = [];
add(product) {
this.productArray.push(product);
}
解决方案
在您的模板中,您必须更改(click)="add()"
为(click)="add(product)"
推荐阅读
- csv - 如何在导出 csv 文件中使用“NULL”而不是“”
- jquery - 如何使用 yammer API 上传图片
- spring - 将 Spring @WebMvcTest 与 Junit5 一起使用时如何将模型属性传递给 Thymeleaf
- amazon-web-services - 在构建期间在 npmrc 中提供令牌
- python - Python - 在异步中取消任务?
- python - python,如何在我的代码中显示龟跑游戏的总分?
- powershell - 如何在 Powershell 中加入两个对象数组
- c# - 在C#中,我设置了DLL的相对路径,但是还是抛出异常,找不到对应的DLL
- visual-studio-code - 如何在 VS 代码中使用内部集成终端而不是 PowerShell?
- marshalling - 导入非托管 dll 并将指针复制到 c# 中的字节数组