ОШИБКА TypeError: _co.shoppingCart.getQuantity не является функцией - PullRequest
0 голосов
/ 27 мая 2018

Я работаю над проектом Angular5 и использую функцию getQuantity в двух разных шаблонах.

В приведенном ниже шаблоне «product-card» getQuantity вызывает ошибку.

ProductCardComponent.html:8 ERROR TypeError: _co.shoppingCart.getQuantity is not a function

В то время как в другом шаблоне "product-amount" он работает нормально.

<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
}

Я не могу найти, почему я получаю эту ошибку.Мне нужно ваше руководство.Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...