无法添加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 回答
Angular 6使用rxjs版本6,catch操作符已更改为catchError,您可以像这样导入
以及如何通过管道使用运算符:
RxJS 6 Changes - Overview
试试这个 :