首页 文章

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

提问于
浏览
0

无法添加catch运算符 . 它给出了'Observable'类型中不存在属性'catch'的错误

[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 回答

  • 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

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

相关问题