首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rxjs@6.3.3的rxjs catchError导入问题

rxjs@6.3.3的rxjs catchError导入问题
EN

Stack Overflow用户
提问于 2019-05-14 12:12:36
回答 1查看 990关注 0票数 2

试图为rxjsv6.3.3导入catchError,但导入看起来不起作用。我在使用catch时出错了。

发现了类似的问题,但看上去对我没有帮助。

代码:

代码语言:javascript
复制
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为我工作

代码语言:javascript
复制
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‘不存在

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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吹成这样。

代码语言:javascript
复制
return this.http.get<IWinServices[]>(this._url).pipe(
    catchError(() => {
       // error handling logic here
    })
)

看这个伟大的网站,以供参考。handling/catch.html

最后注意:不要使用这个import 'rxjs/add/operator/catch';,这是不推荐的,因为它不是一个范围内的导入。

希望这能有所帮助。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56130194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档