类型'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
.
答
是的,您需要导入运营商:
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:
- Angular 2 HTTP GET with TypeScript error http.get(...).map is not a function in [null]