Property 'catch' does not exist on type 'Observable'

后端 未结 3 813
無奈伤痛
無奈伤痛 2020-12-02 07:20

On the Angular 2 documentation page for using the Http service, there is an example.

getHeroes (): Observable {
  return this.http.get(this.ur         


        
3条回答
  •  一整个雨季
    2020-12-02 07:31

    With RxJS 5.5+, the catch operator is now deprecated. You should now use the catchError operator in conjunction with pipe.

    RxJS v5.5.2 is the default dependency version for Angular 5.

    For each RxJS Operator you import, including catchError you should now import from 'rxjs/operators' and use the pipe operator.

    Example of catching error for an Http request Observable

    import { Observable } from 'rxjs';
    import { catchError } from 'rxjs/operators';
    ...
    
    export class ExampleClass {
      constructor(private http: HttpClient) {
        this.http.request(method, url, options).pipe(
          catchError((err: HttpErrorResponse) => {
            ...
          }
        )
      }
      ...
    }
    

    Notice here that catch is replaced with catchError and the pipe operator is used to compose the operators in similar manner to what you're used to with dot-chaining.


    See the rxjs documentation on pipable (previously known as lettable) operators for more info.

提交回复
热议问题