简体   繁体   English

错误类型错误:当我尝试添加产品时无法读取未定义的属性“名称”

[英]ERROR TypeError: Cannot read property 'name' of undefined when i try to add product

I try to make a basket as simply as possible, I try to add a product to my basket but I have an error but I cannot find my error.我尝试尽可能简单地制作一个篮子,我尝试将产品添加到我的篮子中,但我有一个错误,但我找不到我的错误。

thank you谢谢你

html 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.file 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);
  }

In your template, you have to change (click)="add()" to (click)="add(product)"在您的模板中,您必须将(click)="add()"更改为(click)="add(product)"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - “TypeError: Cannot read property 'name' of undefined” error 当我想访问对象详细信息时,React JS引发“ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - React JS throw “TypeError: Cannot read property 'name' of undefined” error when i want to access object details 错误TypeError:当我使用angular2动态添加文本字段时,无法读取未定义的属性“ 0” - ERROR TypeError: Cannot read property '0' of undefined when i add text field dynamiclly with angular2 当我尝试使用tensorflow.js模型进行预测时,出现错误:Uncaught TypeError:无法读取未定义的属性“ length” - When I try to make a prediction using a tensorflow.js model I get the error: Uncaught TypeError: Cannot read property 'length' of undefined 节点REPL中的TypeError:当我向Object.prototype添加属性时,无法读取未定义的属性“ 0” - TypeError in Node REPL: Cannot read property '0' of undefined when I add a property to the Object.prototype Angular 6 中的“错误类型错误:无法读取未定义的属性‘名称’” - "ERROR TypeError: Cannot read property 'name' of undefined" in Angular 6 遇到错误“未捕获的TypeError:无法读取未定义的属性&#39;名称&#39;”的问题 - Difficulty with error “Uncaught TypeError: Cannot read property 'name' of undefined” 错误TypeError:无法读取未定义的属性&#39;名称&#39;-使用angularjs - ERROR TypeError: Cannot read property 'name' of undefined - Using angularjs 类型错误:无法使用 firebase 读取未定义错误的属性“名称” - TypeError: Cannot read property 'name' of undefined error using firebase 类型错误:无法读取未定义反应错误的属性“名称” - TypeError: Cannot read property 'name' of undefined react error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM