rxjs catchError import issue for rxjs@6.3.3



试图导入rxjs v6.3.3的catheror,但导入看起来不起作用。我在使用捕获时会遇到错误。

找到了类似的问题,但看上去没有帮助我。

代码:

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

尝试了可能的解决方案:无适用于我

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)

ERROR in src/app/employee.service.ts(16,52): error TS2339: Property 'catch' does not exist on type 'Observable<IEmployee[]>'

错误解释了问题。

error TS2339: Property 'catch' does not exist on type 'Observable<IEmployee[]>'

在RXJS V6 您不再在可观察的呼叫上链操作员。

而是尝试这个...

类似于以下import { catchError } from 'rxjs/operators';

导入

像这样的catchError管道。


return this.http.get<IWinServices[]>(this._url).pipe(
    catchError(() => {
       // error handling logic here
    })
)

请参阅此大网站以获取参考。https://www.learnrxjs.io/operators/error_handling/catch.html

最终注意:不要使用此 import 'rxjs/add/operator/catch';,不建议使用它,因为它不是范围的导入。

希望这会有所帮助。

最新更新