this.service.myFunction不是函数-打字稿

时间:2019-05-23 21:31:31

标签: angular typescript

我在我的服务班级有这个电话。并且它给出了以下错误。

  

_this.service.myFunction不是函数-打字稿

  constructor(
    private http: HttpClient,
    private service: InventoryService
  ) {}

  getProducts() {
    this.http
      .get<{ message: string; products: any }>(
        'http://localhost:3000/api/products'
      )
      .pipe(
        map(productData => {
          return productData.products.map(product => {
            const currentQty = this.service.myFunction(    //error here
              product.sku
            );
            return {
              sku: product.sku,
              style_code: product.style_code,
              id: product._id,
              current_qty: currentQty
            };
          });
        })
      )
      .subscribe(transformedProducts => {
        this.products = transformedProducts;
        this.productsUpdated.next([...this.products]);
      });
  }

myFunction实现。

  myFunction(sku: string) {
    return this.inventory.find(x => x.sku === sku).current_qty;
  }

0 个答案:

没有答案