19

如何使用 lettable 运算符和管道执行以下操作?

    this.httpClient
      .get(url)
      .map((res: any) => {
        const events = res.eventList;
        return events.map(e => new EventLogModel(e));
      })
      .catch(this.handleError);

我已经尝试过了,但我无法开始catchError工作: catchError does not exist on type Observable<any>:

    this.httpClient
      .get(url)
      .pipe(
        map((res: any) => {
          const events = res.eventList;
          return events.map(e => new EventLogModel(e));
        })
      )
      .catchError(this.handleError);

另外,我假设catch并且catchError是相同的,对吗?我像这样导入它:

import { map, catchError } from 'rxjs/operators';

但我不确定这是否是正确的运算符。

4

1 回答 1

28

您的假设是正确的, lettable 运算符catchError与 相同catch

至于 的放置catchError,它不应该有前缀.,应该放在pipe

this.httpClient
  .get(url)
  .pipe(
    map((res: any) => {
      const events = res.eventList;
      return events.map(e => new EventLogModel(e));
    }),
    catchError(this.handleError);
  )
于 2017-12-01T20:23:00.677 回答