angular - ข้อผิดพลาด TypeError: _co.shoppingCart.getQuantity ไม่ใช่ฟังก์ชัน
ฉันกำลังทำงานในโปรเจกต์ Angular5 และฉันกำลังใช้ฟังก์ชัน getQuantity ในเทมเพลตสองแบบที่แตกต่างกัน
ในเทมเพลต "product-card" ด้านล่าง getQuantity กำลังทำให้เกิดข้อผิดพลาด
ProductCardComponent.html:8 ERROR TypeError: _co.shoppingCart.getQuantity is not a function
ในขณะที่ในเทมเพลตอื่น "product-quantity" มันทำงานได้ปกติ
<div *ngIf="showActions && shoppingCart" class="card-footer">
<button *ngIf="shoppingCart.getQuantity(product) === 0; else updateQuantity"
(click)="addToCart()" class="btn btn-secondary btn-block">Add to Cart</button>
<ng-template #updateQuantity>
<app-product-quantity [product]="product"
[shopping-cart]="shoppingCart"></app-product-quantity>
</ng-template>
</div>
ด้านล่างนี้คือคอมโพเนนต์ ProductCardComponent ของฉัน:
import { ShoppingCart } from './../models/shopping-cart';
import { Product } from './../models/product';
import { Component, OnInit, Input } from '@angular/core';
import { ShoppingCartService } from '../shopping-cart.service';
@Component({
selector: 'app-product-card',
templateUrl: './product-card.component.html',
styleUrls: ['./product-card.component.css']
})
export class ProductCardComponent {
@Input('product') product: Product;
@Input('show-actions') showActions = true;
@Input('shopping-cart') shoppingCart: ShoppingCart;
constructor(private cartService: ShoppingCartService) { }
addToCart() {
this.cartService.addToCart(this.product);
}
}
นี่คือคลาสผู้ช่วยของฉันซึ่งมีเมธอด getQuantity อยู่:
import { ShoppingCartItem } from './shopping-cart-item';
import { Product } from './product';
export class ShoppingCart {
items: ShoppingCartItem[] = [];
constructor(public itemsMap: { [productId: string]: ShoppingCartItem }) {
for (const productId in itemsMap) {
if (productId) {
const item = itemsMap[productId];
if (item) {
this.items.push(new ShoppingCartItem(item.product, item.quantity));
}
}
}
}
getQuantity(product: Product) {
const item = this.itemsMap[product.key];
return item ? item.quantity : 0;
}
// other methods here
}
ฉันไม่สามารถหาสาเหตุได้ว่าทำไมฉันถึงได้รับข้อผิดพลาดนี้ ฉันต้องการคำแนะนำจากคุณ ขอบคุณล่วงหน้า