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

查看:149
本文介绍了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属性地图"在可观察"类型上不存在

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.

我进行了一些搜索:

在角形->我也不使用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类型"Observable< Response>"上不存在属性"map"与问题报告不同->我的命令行对 tsc 一无所知,在完成 npm我打字稿-g (然后重新启动 VS code ng服务

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 有许多运算符可供您使用,例如 map catch do 等,但是为了使用这些您必须引用它们所包含的文件/模块.

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.

有角度的站点上的教程对如何使用 Observable< T> 以及如何为要使用的更常见方法(如 RxJs lib.通过创建引用 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属性'map'在类型'Observable&lt; Response&gt;'上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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