类型'Observable <HttpEvent <any >>'不存在属性'catchError'

alp*_*rim 3 observable rxjs typescript angular rxjs6

从angular 5转到6(使用angular 6和rxjs 6),在我的linter中遇到以下两个错误。任何人都有任何想法,请谢谢。

[ts] 'catchError' is declared but its value is never read.
[ts] Property 'catchError' does not exist on type 'Observable<HttpEvent<any>>'.
Run Code Online (Sandbox Code Playgroud)
[ts] 'catchError' is declared but its value is never read.
[ts] Property 'catchError' does not exist on type 'Observable<HttpEvent<any>>'.
Run Code Online (Sandbox Code Playgroud)

Mic*_*ati 5

这更多是rxjs的变化。您将要熟悉可操作的运算符,但是这里您要进行代码更改...

import { Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';



@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(authReq)
      .pipe(catchError((error, caught) => {
        console.log('Error Occurred');
        console.log(error);
        return Observable.throw(error);
      })) as any;
  }
}
Run Code Online (Sandbox Code Playgroud)

很简单吧!现在,大多数rxjs运算符都传递给pipe了可观察函数!

  • `Observable.throw`-&gt;`throwError`。 (4认同)