我正在使用带有TypeScript(版本1.6)的Angular(版本2),当我编译代码时,我得到以下错误:
Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/change_detection/parser/locals.d.ts(4,42): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(1,25): Error TS2304: Cannot find name 'MapConstructor'.
node_modules/angular2/src/core/facade/collection.d.ts(2,25): Error TS2304: Cannot find name 'SetConstructor'.
node_modules/angular2/src/core/facade/collection.d.ts(4,27): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(4,39): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(7,9): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(8,30): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(11,43): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(12,27): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(14,23): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(15,25): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/collection.d.ts(94,41): Error TS2304: Cannot find name 'Set'.
node_modules/angular2/src/core/facade/collection.d.ts(95,22): Error TS2304: Cannot find name 'Set'.
node_modules/angular2/src/core/facade/collection.d.ts(96,25): Error TS2304: Cannot find name 'Set'.
node_modules/angular2/src/core/facade/lang.d.ts(1,22): Error TS2304: Cannot find name 'BrowserNodeGlobal'.
node_modules/angular2/src/core/facade/lang.d.ts(33,59): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/facade/promise.d.ts(1,10): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(3,14): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(8,32): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(9,38): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(10,35): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(10,93): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(11,34): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(12,32): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(12,149): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/facade/promise.d.ts(13,43): Error TS2304: Cannot find name 'Promise'.
node_modules/angular2/src/core/linker/element_injector.d.ts(72,32): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/element_injector.d.ts(74,17): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/element_injector.d.ts(78,184): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/element_injector.d.ts(83,182): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/element_injector.d.ts(107,37): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/proto_view_factory.d.ts(27,146): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(52,144): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(76,79): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(77,73): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(94,31): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(97,18): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(100,24): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(103,142): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/linker/view.d.ts(104,160): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/render/api.d.ts(281,74): Error TS2304: Cannot find name 'Map'.
node_modules/angular2/src/core/zone/ng_zone.d.ts(1,37): Error TS2304: Cannot find name 'Zone'.
这是代码:
import 'reflect-metadata';
import {bootstrap, Component, CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/core';
@Component({
selector: 'my-app',
template: '<input type="text" [(ng-model)]="title" /><h1>{{title}}</h1>',
directives: [ CORE_DIRECTIVES ]
})
class AppComponent {
title :string;
constructor() {
this.title = 'hello angular 2';
}
}
bootstrap(AppComponent);
23 回答
我在Angular 2的网站上找到了this very helpful doc . 它最终让我让事情正常工作,而另一个答案在这里,安装打字,让我失败了各种错误 . (但帮助我朝着正确的方向前进 . )
它不包括es6-promise和es6-collections,而是包含core-js,它为我做了诀窍......与Angular2的核心ts定义没有冲突 . 此外,该文档还解释了如何在安装NPM时自动设置所有这些,以及如何修改typings.json文件 .
并为index.d.ts添加正确的路径
试过@ angular-2.0.0-rc3
一个已知问题:https://github.com/angular/angular/issues/4902
核心原因:TypeScript隐式包含的
.d.ts
文件因编译目标而异,因此在定位es5
时需要有更多环境声明,即使实际存在于运行时(例如chrome)中 . 更多关于lib.d.ts在应用程序的主文件夹中添加typing.d.ts,并在那里声明每次都要使用的变量
在type.d.ts中声明这个之后,require的错误将不会出现在应用程序中 .
现在您必须手动导入它们 .
我在Angular 2 rc1上得到了这个 . 结果显示一些名字随着打字v1和旧的0.x而改变 .
browser.d.ts
文件变成index.d.ts
.运行
typings install
找到你的启动文件(你引导的地方)并添加:/// <reference path="../typings/index.d.ts" />
(如果您的启动文件与typings文件夹位于同一文件夹中,则不带../
)将
index.d.ts
添加到tsconfig.json
中的文件列表中由于某种原因无效 .此外,不需要
es6-shim
包 .我有同样的问题,我使用
tsconfig.json
中的lib
选项解决了它 . 如basarat in his answer所述,TypeScript隐式包含.d.ts
文件,具体取决于compiletarget
选项,但可以使用lib
选项更改此行为 .您可以指定要包含的其他定义文件,而无需更改目标JS版本 . 例如,这是我当前
compilerOptions
的Typescript@2.1的一部分,它增加了对es2015
功能的支持,而无需安装任何其他内容:有关可用选项的完整列表,请查看official doc .
另请注意,我在代码中添加了
"types": ["node"]
并安装了npm install @types/node
以支持require('some-module')
.对于
Angular 2.0.0-rc.0
添加node_modules/angular2/typings/browser.d.ts
将无效 . 首先使用以下内容将typings.json file添加到您的解决方案中:然后更新
package.json
文件以包含此postinstall
:现在运行
npm install
此外,您现在也应该忽略
tsconfig.json
文件中的typings
文件夹:Update
现在AngularJS 2.0正在使用
core-js
而不是es6-shim
. 按照快速启动typings.json file获取更多信息 .如果
npm install -g typings typings install
仍然没有帮助,请在执行此命令之前删除node_modules和typings文件夹 .Angular 2 RC1将
node_modules/angular2
目录重命名为node_modules/angular
.如果您正在使用gulpfile将文件复制到输出目录,那么您可能仍然会坐在那里,这可能会被编译器拾取并使其自身混乱 .
因此(仔细)消除
node_modules
中的测试版本,并删除任何旧版本并重新运行typings install
.好的电话,@ VahidN,我发现我需要
在我的
tsconfig
中,也是为了阻止es6-shim
本身的错误更新
tsconfig.json
,更改至
接受的答案没有提供可行的解决方案,而且大多数其他的建议不再适用"triple-slashes"解决方法,因为Angular2最新的RC已经删除了
browser.d.ts
,因此不再可用 .我强烈建议按照这里的几个解决方案的建议安装
typings
模块,但它只是一个有效的方法,只为你的项目和VS2015界面做到这一点 . 这是你需要做的:在项目的package.json文件中添加
typings
.在
package.json
文件中添加script
块,以在每次NPM操作后执行/更新typings
.在项目的根文件夹中添加
typings.json
文件,其中包含对core-js
的引用(总体优于es6-shim
atm) .而已 .
您还可以在我的博客上查看this other SO thread和/或阅读this post以获取更多详细信息 .
我能够使用以下命令解决此问题
请注意,您需要
typings
才能使上述命令正常工作,如果您没有它,请运行以下命令进行安装UPDATE
typings update不读
--ambient
它也成了--global
对于你需要安装上述库的定义的一些人,只需使用以下命令感谢 @bgerth 指出这一点 .
我能够通过安装
typings
模块来解决这个问题 .(使用ng 2.0.0-rc.1)
请在JS文件中导入以下语句 .
当Typescript> = 2时,tsconfig.json中的"lib"选项将完成这项工作 . 不需要打字 . https://www.typescriptlang.org/docs/handbook/compiler-options.html
这就是想想每个人都在尝试做些不同的事情 . 我相信这些系统仍然远离最终版本 .
在我的情况下,我从rc.0更新到rc.5出现了这个错误 .
我修复了更改tsconfig.json .
我是Angular的新手,但对我来说,只需输入Input即可找到解决方案 . 不能归功于这个,在另一个板上找到它 . 如果您遇到同样的问题,这是一个简单的修复,但如果您的问题更复杂,我会阅读上面的内容 .
穆克什:
在定位ES5时,未定义承诺等ES6功能 . 还有其他库,但core-js是Angular团队使用的javascript库 . 它含有ES6的填充物 .
自问这个问题以来,Angular 2发生了很大的变化 . 类型声明在Typescript 2.0中更容易使用 much .
对于Angular 2中的ES6功能,您不需要键入 . 只需使用typescript 2.0或更高版本,并使用npm安装@ types / core-js:
然后,将 TypeRoots 和 Types 属性添加到tsconfig.json:
这比使用Typings容易得多,如其他答案中所述 . 有关详细信息,请参阅Microsoft的博客文章:Typescript: The Future of Declaration Files
对于那些遵循
angular.io
上的Angular2教程的人来说,只是为了明确,这里是对mvdluit的答案的扩展,确切地说放置代码的位置:你的
main.ts
应该是这样的:请注意,您留下
///
正斜杠,不要删除它们 .参考:https://github.com/ericmdantas/angular2-typescript-todo/blob/master/index.ts#L1
您可以在.ts文件的开头添加代码 .
在2.0.0-beta.6 (2016-02-11)的变更日志中提到了一种解决方法(在重大变化下列出):
确保你有正确的参考路径,我需要在开始时添加 ../ 以使其正常工作 .