,angular,rxjs,httpclient"/>

首页 > 解决方案 > 为什么我的 httpclient 中的 rxjs 管道函数返回 Observable

问题描述

在这段代码中

getHero(id: number): Observable<Hero> {
  const url = `${this.heroesUrl}/${id}`;
  return this.http.get<Hero>(url).pipe(
    tap(_ => this.log(`fetched hero id=${id}`)),
    catchError(this.handleError<Hero>(`getHero id=${id}`))
  );
}

从这里的Angular文档 工作正常,而在我的项目中,“相同”的代码给了我错误

错误 TS2322:类型“Observable”不可分配给类型“Observable”。

升级到 Angular 6 后?

public getCustomer(customerRecId: string): Observable<Customer> {
  const url = `${this.baseUrl}/Customer?recId=${customerRecId}`;
  const customer$ = this.http.get(url, this.httpOptions).pipe(
    map(this.mapCustomer), 
    catchError(this.handleError)
  );

  return customer$;
}

将鼠标悬停在 vsCode 中的“.pipe”上显示在前一个示例中它返回一个 Observable 但在后者中返回一个 Observable

我不明白为什么这两个例子不同。

标签: angularrxjshttpclient

解决方案


推荐阅读