简体   繁体   中英

error TS2339: Property 'catch' does not exist on type 'Observable<empInterface[]>'

Not able to add catch operator. It gives error that Property 'catch' does not exist on type 'Observable

[enter image description here][1]

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { empInterface } from './empInterface';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/catch';

@Injectable({
    providedIn:'root'
})
export class DynamicempService {
    private _url: string="/assets/data/employeeDb.json";
    constructor(private localData: HttpClient) { }  

    getEmployee(): Observable<empInterface[]>{
        return this.localData.get<empInterface[]> 
        (this._url).catch(this.errorMethod);
    }

    errorMethod(error: HttpErrorResponse){
        return Observable.throw(error.message || "Server Error");
    }
}

Angular 6 use rxjs version 6 and catch operator has been changed to catchError and you can Imported like this

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

and this how you can use the operators by pipe :

import { map } from 'rxjs/operators';

myObservable
  .pipe(map(data => data * 2))
  .subscribe(...);

RxJS 6 Changes - Overview

Try this :

  import { Observable, pipe } from 'rxjs';
  import { _throw } from 'rxjs/observable/throw';
  import { catchError } from 'rxjs/operators';

  getEmployee(): Observable<empInterface[]>{
    return this.localData.get<empInterface[]> 
    (this._url).pipe(
       catchError(this.errorMethod)
    );
  }

  errorMethod(error: HttpErrorResponse){
    return _throw(error.message || "Server Error");
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM