这是我的 appservice.ts 中的一个函数,用于从本地存储中获取产品
getproducts(){
const storedCart = this.storage.get(this.key);
if (storedCart != null) {
this.mycart = storedCart;
return this.mycart;
}
else{
return null;
}
}
这是我的购物车 component.ts,我在其中使用 appservice 调用该方法
export class CartComponent implements OnInit {
carts:Cart[]=[];
constructor(private appservice:AppService) { }
getcartproducts(){
this.carts=this.appservice.getproducts();//calling appservice
}
ngOnInit() {
this.getcartproducts();
}}
现在在我的 cartcomponent.html 中
<tr class="rem1" valign="middle" *ngFor="let cart of carts;let i = index">
<div class="entry value"><span>{{cart[i].quantity}}</span></div>
</tr>
上面的代码给我的错误是
cannot read property 'quantity' of undefined
我不知道我哪里做错了,任何帮助将不胜感激!!
最佳答案
cart 是一个对象,您没有对象的索引,将其更改为
<div class="entry value"><span>{{cart?.quantity}}</span></div>
关于javascript - component.html 中的变量在 Angular 6 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579521/