错误TS2339:类型'Observable <empinterface []>'上不存在属性'catch'

时间:2018-05-31 09:04:28

标签: angular angular6

无法添加catch操作符。它给出了属性“捕获”的错误 类型'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");
    }
}

2 个答案:

答案 0 :(得分:1)

Angular 6使用rxjs版本6并且catch操作符已更改为catchError,您可以像这样导入

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

以及如何通过管道使用运算符:

import { map } from 'rxjs/operators';

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

RxJS 6 Changes - Overview

答案 1 :(得分:0)

试试这个:

  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");
  }