首页 > 解决方案 > 错误 TS2339:“可观察”类型上不存在属性“捕获”'

问题描述

无法添加 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");
    }
}

标签: angularangular6

解决方案


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 变化 - 概述


推荐阅读