小编use*_*483的帖子

订阅'不能分配给类型

我在获取服务以在页面上显示结果时遇到问题.错误是订阅方法返回订阅类型,我不知道试图将它带到产品数组.产品位于json文件中.

设置:我正在尝试通过阅读教程来学习Angular 2.教程已过时,我正在使用最新版本的angular(ng -v = @ angular/cli:1.4.2).我使用ng new和ng generate来设置应用程序.

产品list.component.ts

export class ProductListComponent implements OnInit {

  pageTitle = 'Product List';
  imageWidth = 50;
  imageMargin = 2;
  showImage = false;
  listFilter = '';
  products: IProductList[];
  subscription: Subscription;
  errorMessage = '';

  constructor(private _productListService: ProductListService) {
  }

  ngOnInit() {
**// ERROR - 'Subscription' is not assignable to type 'IProductList[]'**
    this.products = this._productListService.getProducts()  // ******** ERROR ******
      .subscribe(
        products => this.products = products,
        error => this.errorMessage = <any>error);
  }
Run Code Online (Sandbox Code Playgroud)

产品list.service.ts

import { Injectable } …
Run Code Online (Sandbox Code Playgroud)

observable angular

4
推荐指数
1
解决办法
4352
查看次数

标签 统计

angular ×1

observable ×1