【问题标题】:Subscription' is not assignable to type订阅'不可分配给类型
【发布时间】:2017-11-25 06:47:30
【问题描述】:

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

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

product-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);
  }

product-list.service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { IProductList } from './product-list';


@Injectable()
export class ProductListService {
  private _productListUrl = 'api/product-list/product-list.json';

  constructor(private _http: Http) { }

  getProducts(): Observable<IProductList[]> {
    return this._http.get(this._productListUrl)
            .map((response: Response) => <IProductList[]>response.json())
            .do(data => console.log('All: ' + JSON.stringify(data)))
            .catch(this.handleError);
  }

  private handleError(error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server Error');
  }
}

product-list.component.html

    <tr *ngFor='let product of products | async | productFilter: listFilter' >
      <td>
        <img *ngIf='showImage' [src]='product.imageUrl' [title]='product.productName' [style.width.px]='imageWidth' [style.marging.px]='imageMargin'>
      </td>
      <td>{{product.productName}}</td>
      <td>{{product.productCode | lowercase }}</td>
      <td>{{product.releaseDate}}</td>
      <td>{{product.price | currency:'USD':true:'1.2-2' }}</td>
      <td><app-ai-star [rating] = 'product.starRating'
           (ratingClicked)='onRatingClicked($event)'></app-ai-star></td>
    </tr>

【问题讨论】:

    标签: angular observable


    【解决方案1】:

    问题是您希望订阅 = getProducts() 电话。

    ngOnInit() {
        this.subscription = this._productListService.getProducts() // subscription created here
          .subscribe(
            products => this.products = products, // value applied to products here
            error => this.errorMessage = <any>error);
      }
    

    【讨论】:

      【解决方案2】:

      this.products 的值在subscribe() 中分配。您可以忽略返回的订阅对象,如下面的代码 sn-p 所示。

      ngOnInit() {
          this._productListService.getProducts() 
            .subscribe(
              products => this.products = products,
              error => this.errorMessage = <any>error);
        }
      

      【讨论】:

        猜你喜欢
        • 2019-08-01
        • 2016-11-12
        • 1970-01-01
        • 2017-05-06
        • 2023-01-09
        • 2018-10-12
        • 2020-09-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多