试图为rxjsv6.3.3导入catchError,但导入看起来不起作用。我在使用catch时出错了。
发现了类似的问题,但看上去对我没有帮助。
代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { IWinServices } from './WinServices';
import { Observable } from 'rxjs';
import { catch } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class WinServicesService {
private _url : string = './assets/Data/WinServicess.json'
constructor(private http: HttpClient) { }
getWinServices() :Observable <IWinServices[]> {
return this.http.get<IWinServices[]>(this._url).catch (this.errorHandler);
}
errorHandler(error: HttpErrorResponse) {
return Observable.throw(error.message || "Server Error");
}
}尝试了可能的解决方案: None为我工作
import { catchError } from 'rxjs/operators';
import 'rxjs/add/operator/catch';
import {Observable} from 'rxjs/Rx';错误:
Property 'catch' does not exist on type Observable<IWinServices[]>'.ts(2339)src/app/Employee.service.ts(16,52)中的错误:错误TS2339:属性'Observable‘不存在
发布于 2019-05-14 12:40:31
这个错误解释了这个问题。
error TS2339: Property 'catch' does not exist on type 'Observable<IEmployee[]>'
In rxjs v6+您不再将运算符链接到可观察的调用.上。
不如试试这个..。
导入如下所示的import { catchError } from 'rxjs/operators';
把catchError吹成这样。
return this.http.get<IWinServices[]>(this._url).pipe(
catchError(() => {
// error handling logic here
})
)看这个伟大的网站,以供参考。handling/catch.html
最后注意:不要使用这个import 'rxjs/add/operator/catch';,这是不推荐的,因为它不是一个范围内的导入。
希望这能有所帮助。
https://stackoverflow.com/questions/56130194
复制相似问题