У меня проблема при добавлении товара в корзину, его нужно добавить или удалить, но ничего не было сделано, если я допустил ошибку, которую я не знаю, потому что после компиляции в браузере я получаю эту ошибку
Невозможно прочитать свойство '0' из неопределенного на ProductCardComponent.push ../ src / app / product-card / product-card.component.ts.ProductCardComponent.getQuantity
эта ошибка указывает на проблему в html product-card.component, ts файлы
product-card.component.ts
export class ProductCardComponent {
// tslint:disable-next-line:no-input-rename
@Input('product') product: Product;
// tslint:disable-next-line:no-input-rename
@Input('show-actions') showActions = true;
// tslint:disable-next-line: no-input-rename
@Input('shopping-cart') shoppingCart;
constructor(private cartService: ShoppingCartService) {}
addToCart(product: Product) {
this.cartService.addToCart(product);
}
getQuantity() {
// tslint:disable-next-line:curly
if (!this.shoppingCart) return 0;
**here i am getting error**
const item = this.shoppingCart.items[this.product.$key];
return item ? item.quantity : 0;
}
}
product-card.component.html
<div *ngIf="showActions" class="card-footer">
**here i am getting error**
<button
*ngIf="getQuantity() === 0; else updateQuantity"
(click)="addToCart(product)"
class="btn btn-secondary btn-block"
>
Add to Cart
</button>
<ng-template #updateQuantity>
<div class="row">
<div class="col-2">
<button class="btn btn-secondary btn-block">-</button>
</div>
<div class="col">
{{ getQuantity() }}
</div>
<div class="col-2">
<button class="btn btn-secondary btn-block">+</button>
</div>
</div>
</ng-template>
</div>