'Observable<any>' 类型上不存在属性 'catch' [英] Property 'catch' does not exist on type 'Observable<any>'

查看:19
本文介绍了'Observable<any>' 类型上不存在属性 'catch'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Http 服务的 Angular 2 文档页面上,有一个示例.

On the Angular 2 documentation page for using the Http service, there is an example.

getHeroes (): Observable<Stuff[]> {
  return this.http.get(this.url)
                  .map(this.extractData)
                  .catch(this.handleError);
}

我克隆了 angular2-webpack-starter 项目并自己添加了上述代码.

I cloned the angular2-webpack-starter project and added the above code myself.

我使用

import {Observable} from 'rxjs/Observable';

我假设属性 Observable 也被导入(.map 有效).查看 rxjs.beta-6 的更新日志,没有提到 catch.

I'm assuming the properties Observable are imported as well (.map works). Looked at the changelog for rxjs.beta-6 and nothing is mentioned about catch.

推荐答案

警告:此解决方案自 Angular 5.5 起已弃用,请参考下方 Trent 的回答

Warning: This solution is deprecated since Angular 5.5, please refer to Trent's answer below

======================

=====================

是的,您需要导入运算符:

Yes, you need to import the operator:

import 'rxjs/add/operator/catch';

或者这样导入Observable:

import {Observable} from 'rxjs/Rx';

但在本例中,您导入所有运算符.

But in this case, you import all operators.

查看此问题了解更多详情:

See this question for more details:

这篇关于'Observable&lt;any&gt;' 类型上不存在属性 'catch'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆