我刚从Angular 2 beta16 升级到 beta17 ,后者又需要rxjs 5.0.0-beta.6 . (更改日志:https://github.com/angular/angular/blob/master/CHANGELOG.md#200-beta17-2016-04-28)在beta16中,所有关于Observable / map功能都运行良好 . 我升级后出现以下错误,当打字稿尝试转换时出现:
属性'map'在类型'Observable'上不存在(我在 Map 中使用了带有observable的 Map )c:/path/node_modules/rxjs/add/operator/map.d.ts(2,16):error TS2435:环境模块不能嵌套在其他模块或命名空间中 . c:/path/node_modules/rxjs/add/operator/map.d.ts(2,16):错误TS2436:环境模块声明无法指定相对模块名称 .
我看过这个问题/答案,但它没有解决问题:Observable errors with Angular2 beta.12 and RxJs 5 beta.3
我的appBoot.ts看起来像这样(我已经引用了rxjs / map):
///<reference path="./../node_modules/angular2/typings/browser.d.ts"/>
import {bootstrap} from "angular2/platform/browser";
import {ROUTER_PROVIDERS} from 'angular2/router';
import {HTTP_PROVIDERS} from 'angular2/http';
[stuff]
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import {enableProdMode} from 'angular2/core';
import { Title } from 'angular2/platform/browser';
//enableProdMode();
bootstrap(AppDesktopComponent, [
ROUTER_PROVIDERS,
HTTP_PROVIDERS,
Title
]);
有没有人知道什么是乱七八糟的?
15 回答
您需要导入
map
运算符:从版本5转换到6时,将弹出类似的错误消息 . 以下是对rxjs-6的更改的答案 .
导入各个运算符,然后使用
pipe
而不是链接 .我将我的gulp-typescript插件升级到最新版本(2.13.0),现在它编译顺利 .
更新1:我之前使用的是gulp-typescript版本2.12.0
更新2:如果要升级到Angular 2.0.0-rc.1,则需要在appBoot.ts文件中执行以下操作:
重要的是引用es6-shim / index.d.ts
假设您已经安装了es6-shim类型,如下所示:
更多关于Angular的打字安装:https://angular.io/docs/ts/latest/guide/typescript-configuration.html#!#typings
Rxjs 5.5“属性' Map '在类型Observable上不存在 .
问题与您需要在所有运营商周围添加管道有关 .
改变这个,
对此
和
导入这样的 Map ,
它将解决您的问题 .
在我的情况下,仅包括 Map 和承诺是不够的:
我通过导入几个rxjs组件解决了这个问题,因为official documentation建议:
1)在一个app / rxjs-operators.ts文件中导入语句:
2)在您的服务中导入rxjs-operator本身:
如果您碰巧在VS2015中看到此错误,则会出现github问题和解决方法:
https://github.com/Microsoft/TypeScript/issues/8518#issuecomment-229506507
这确实帮助我解决了
property map does not exist on observable
问题 . 此外,请确保您的打字稿版本高于1.8.2对于使用角度6的人的最终答案:
在 *.service.ts 文件中添加以下命令“
我正在使用Windows 10;
angular6与打字稿V 2.3.4.0
&
npm install rxjs@6 rxjs-compat@6 --save
为我工作
在Angular 2x中
在example.component.ts中
在example.component.html中
在Angular 5x或Angular 6x中:
在example.component.ts中
在example.component.html中
UPDATE Sep 29 2016 for Angular 2.0 Final & VS 2015
不再需要解决方法,解决您只需要install TypeScript version 2.0.3 .
修复this github issue comment上的编辑 .
正如Justin Scofield在他的回答中建议的那样,对于Angular 5的最新版本和Angular 6,如同2018年6月1日,只是
import 'rxjs/add/operator/map';
不足以消除TS错误:[ts] Property 'map' does not exist on type 'Observable<Object>'.
必须运行以下命令来安装所需的依赖项:
npm install rxjs@6 rxjs-compat@6 --save
之后map
import依赖性错误得到解决!看起来最新的RxJS需要typescript 1.8,因此typescript 1.7报告了上述错误 .
我通过升级到最新的打字稿版本解决了这个问题 .
据我所知,这是因为
rxjs
上次更新 . 他们改变了一些运算符和语法 . 此后我们应该像这样导入rx
运算符import { map } from "rxjs/operators";
而不是这个
我们需要在这样的所有运营商周围添加管道
this.myObservable().pipe(map(data => {}))
来源是here
我面临着类似的错误 . 当我做这三件事时它就解决了:
npm install rxjs@6 rxjs-compat@6 --save
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/toPromise';
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent } from 'rxjs';
如果您正在使用angular4,请使用以下导入 . 它应该工作 .
import 'rxjs/add/operator/map';
如果您使用angular5 / 6,则使用带管道的 Map 并在下面导入
import from "rxjs/operators";