ts 属性 'map'不存在于类型 'Observable<Response>' [英] ts Property 'map' does not exist on type 'Observable<Response>'

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

问题描述

我是 angular 和 npm 的新手,但是在学习时,我遇到了一个错误,其中的代码似乎适用于我正在尝试重现的源代码(此处).

I'm new to angular and npm, but when learning, I got an error, with a code that seems to work on the source I'm trying to reproduce(here).

我认为这与我的开发环境有关,但我不明白为什么.

I assume this has to do with my development environment but I can't exactly understand why.

这是我的代码:

return this.http.get('api/cards.json')
 .map((response:Response) => <Card[]>response.json().data)
 .do(data=>console.log(data))
 .catch(this.handleError);

.map 上,Visual Studio Code 给我这个错误:

On the .map, Visual Studio Code give me this error:

ts 属性 'map' 不存在于类型 'Observable'

ts Property 'map' does not exist on type 'Observable'

如果我忽略此错误并转到浏览器,则会收到相同的消息.

If I ignore this error and go to the browser, I've the same message.

我用 angular-cli 创建了我的项目,我的 angular-version 实际上是 2.4.4.服务器正在使用 ng serve 工具运行.

I've created my project with the angular-cli, and my angular-version is actually the 2.4.4. The server is running with the ng serve tool.

我进行了一些搜索:

属性映射不存在于 observable 响应中角度 -->我不使用 Visual Studio 2015,而且我是最新的 Visual Studio Code(而且似乎这个错误已经解决了一段时间).

property map does not exist on observable response in angular --> I don't use visual studio 2015, also, I'm up to date with my Visual Studio Code(also it seems the bug has ben solved since a while).

Angular 2 2.0.0-rc.1 属性地图"不存在于可观察<响应>"类型上与问题报告不同 -->我的命令行对 tsc 一无所知,并且在我完成了 npm i typescript -g(并重新启动了 VS codeng serve

Angular 2 2.0.0-rc.1 Property 'map' does not exist on type 'Observable<Response>' not the same as issue report --> My commandline doesn't know anything about tsc and after I've done the npm i typescript -g(and restarted both VS code and the ng serve

知道出了什么问题(我想有些东西不是最新的,但我不知道是什么以及如何更新它).

Any idea what is going wrong(I suppose something is not up to date, but I don't know what and how to update it).

推荐答案

你必须在你的模块/组件/服务中引用 RxJs 库(无论你在这里写的是什么) 作为 Observable(从 http.get 返回的类型)是该库的一部分.RxJs 有很多你可以使用的操作符,比如 mapcatchdo 等等,但是为了使用您必须引用它们所在的文件/模块.

You have to reference the RxJs library in your module/component/service (whatever it is you are writing here) as Observable (the type returned from http.get) is a part of that library. The RxJs has many operators that you can use like map, catch, do, etc but in order to use these you must reference the files/modules that they are contained in.

angular 站点上的教程很好地解释了如何使用 Observable 以及如何创建引用映射到更常用的方法,例如 Observable代码>RxJs 库.通过创建一个包含对 RxJs 库中更常用的运算符和类型的引用的单个文件,您只需在要使用这些类型的地方引用该引用文件,就无需重新添加项目中每个文件中您想要利用它们的所有运算符/类型.

The tutorials on the angular site have a good explanation on how you consume the Observable<T> and how to create a reference mapping to the more common methods you want to use like map in the RxJs lib. By creating a single file with references to the more commonly used operators and types in the RxJs library you only have to then reference that reference file where you want to consume those types which saves on having to re-add all the operators/types in every file across your project where you want to take advantage of them.

这是一个示例文件(在本示例中名为 rxjs-operators.ts),其中包含一些更常用的方法.

Here is an example file (named rxjs-operators.ts for this example) with some of the more commonly used methods.

// Observable class extensions
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/throw';

// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';

在您要使用 .map(或任何其他方法)的文件顶部添加此行.

To the top of your file you want to use .map (or any other method) add this line.

import './rxjs-operators';

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

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