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