【问题标题】:error TS2339: Property 'catch' does not exist on type 'Observable<empInterface[]>'错误 TS2339:“Observable<empInterface[]>”类型上不存在属性“catch”
【发布时间】:2018-05-31 17:18:53
【问题描述】:

无法添加 catch 运算符。它给出了属性“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");
    }
}

【问题讨论】:

    标签: angular angular6


    【解决方案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

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-29
        • 2018-10-25
        • 2018-11-03
        • 1970-01-01
        • 2019-03-02
        • 1970-01-01
        相关资源
        最近更新 更多