首页 文章

Angular和Typescript:找不到名字

提问于
浏览
197

我正在使用带有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 回答

  • 10

    我在Angular 2的网站上找到了this very helpful doc . 它最终让我让事情正常工作,而另一个答案在这里,安装打字,让我失败了各种错误 . (但帮助我朝着正确的方向前进 . )

    它不包括es6-promise和es6-collections,而是包含core-js,它为我做了诀窍......与Angular2的核心ts定义没有冲突 . 此外,该文档还解释了如何在安装NPM时自动设置所有这些,以及如何修改typings.json文件 .

  • 103
    typings install dt~es6-shim --save --global
    

    并为index.d.ts添加正确的路径

    ///<reference path="../typings/index.d.ts"/>
    

    试过@ angular-2.0.0-rc3

  • 3

    一个已知问题:https://github.com/angular/angular/issues/4902

    核心原因:TypeScript隐式包含的 .d.ts 文件因编译目标而异,因此在定位 es5 时需要有更多环境声明,即使实际存在于运行时(例如chrome)中 . 更多关于lib.d.ts

  • 7

    在应用程序的主文件夹中添加typing.d.ts,并在那里声明每次都要使用的变量

    declare var System: any;
    declare var require: any;
    

    在type.d.ts中声明这个之后,require的错误将不会出现在应用程序中 .

  • 4

    现在您必须手动导入它们 .

    import 'rxjs/add/operator/retry';
    import 'rxjs/add/operator/timeout';
    import 'rxjs/add/operator/delay';
    import 'rxjs/add/operator/map';
    
    
    
    
    this.http.post(url, JSON.stringify(json), {headers: headers, timeout: 1000})
    
             .retry(2)
    
             .timeout(10000, new Error('Time out.'))
    
             .delay(10)
    
             .map((res) => res.json())
            .subscribe(
              (data) => resolve(data.json()),
              (err) => reject(err)
            );
    
  • 1

    我在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 包 .

  • 7

    我有同样的问题,我使用 tsconfig.json 中的 lib 选项解决了它 . 如basarat in his answer所述,TypeScript隐式包含 .d.ts 文件,具体取决于compile target 选项,但可以使用 lib 选项更改此行为 .

    您可以指定要包含的其他定义文件,而无需更改目标JS版本 . 例如,这是我当前 compilerOptions 的Typescript@2.1的一部分,它增加了对 es2015 功能的支持,而无需安装任何其他内容:

    "compilerOptions": {
        "experimentalDecorators": true,
        "lib": ["es5", "dom", "es6", "dom.iterable", "scripthost"],
        "module": "commonjs",
        "moduleResolution": "node",
        "noLib": false,
        "target": "es5",
        "types": ["node"]
    }
    

    有关可用选项的完整列表,请查看official doc .

    另请注意,我在代码中添加了 "types": ["node"] 并安装了 npm install @types/node 以支持 require('some-module') .

  • 32

    对于 Angular 2.0.0-rc.0 添加 node_modules/angular2/typings/browser.d.ts 将无效 . 首先使用以下内容将typings.json file添加到您的解决方案中:

    {
        "ambientDependencies": {
            "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
        }
    }
    

    然后更新 package.json 文件以包含此 postinstall

    "scripts": {
        "postinstall": "typings install"
    },
    

    现在运行 npm install

    此外,您现在也应该忽略 tsconfig.json 文件中的 typings 文件夹:

    "exclude": [
            "node_modules",
            "typings/main",
            "typings/main.d.ts"
        ]
    

    Update

    现在AngularJS 2.0正在使用 core-js 而不是 es6-shim . 按照快速启动typings.json file获取更多信息 .

  • 2

    如果 npm install -g typings typings install 仍然没有帮助,请在执行此命令之前删除node_modules和typings文件夹 .

  • 0

    Angular 2 RC1将 node_modules/angular2 目录重命名为 node_modules/angular .

    如果您正在使用gulpfile将文件复制到输出目录,那么您可能仍然会坐在那里,这可能会被编译器拾取并使其自身混乱 .

    因此(仔细)消除 node_modules 中的测试版本,并删除任何旧版本并重新运行 typings install .

  • 2

    好的电话,@ VahidN,我发现我需要

    "exclude": [
            "node_modules",
            "typings/main",
            "typings/main.d.ts"
        ]
    

    在我的 tsconfig 中,也是为了阻止 es6-shim 本身的错误

  • 4

    更新 tsconfig.json ,更改

    "target": "es5"
    

    "target": "es6"
    
  • 2

    接受的答案没有提供可行的解决方案,而且大多数其他的建议不再适用"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以获取更多详细信息 .

  • 15

    我能够使用以下命令解决此问题

    typings install es6-promise es6-collections --ambient
    

    请注意,您需要 typings 才能使上述命令正常工作,如果您没有它,请运行以下命令进行安装

    npm install -g typings
    

    UPDATE

    typings update不读 --ambient 它也成了 --global 对于你需要安装上述库的定义的一些人,只需使用以下命令

    typings install dt~es6-promise dt~es6-collections --global --save
    

    感谢 @bgerth 指出这一点 .

  • 80

    我能够通过安装 typings 模块来解决这个问题 .

    npm install -g typings
    typings install
    

    (使用ng 2.0.0-rc.1)

  • 4

    请在JS文件中导入以下语句 .

    import 'rxjs/add/operator/map';
    
  • 13

    当Typescript> = 2时,tsconfig.json中的"lib"选项将完成这项工作 . 不需要打字 . https://www.typescriptlang.org/docs/handbook/compiler-options.html

    {
        "compilerOptions": {
            "target": "es5",
            "lib": ["es2016", "dom"] //or es6 instead of es2016(es7)
        }
    }
    
  • 4

    这就是想想每个人都在尝试做些不同的事情 . 我相信这些系统仍然远离最终版本 .

    在我的情况下,我从rc.0更新到rc.5出现了这个错误 .

    我修复了更改tsconfig.json .

    {
        "compilerOptions": {
            "target": "es6", <-- It was es5
            "module": "system",
            "moduleResolution": "node",
            "sourceMap": true,
            "emitDecoratorMetadata": true,
            "experimentalDecorators": true,
            "removeComments": false,
            "noImplicitAny": false,
            "outDir": "../wwwroot/app"
        },
        "compileOnSave": true,
        "exclude": [
            "../node_modules",
            "../typings/main"
        ]
    }
    
  • 49

    我是Angular的新手,但对我来说,只需输入Input即可找到解决方案 . 不能归功于这个,在另一个板上找到它 . 如果您遇到同样的问题,这是一个简单的修复,但如果您的问题更复杂,我会阅读上面的内容 .

    穆克什:

    你必须输入这样的输入儿童组件的顶部

    import { Directive, Component, OnInit, Input } from '@angular/core';
    
  • -3

    在定位ES5时,未定义承诺等ES6功能 . 还有其他库,但core-js是Angular团队使用的javascript库 . 它含有ES6的填充物 .

    自问这个问题以来,Angular 2发生了很大的变化 . 类型声明在Typescript 2.0中更容易使用 much .

    npm install -g typescript
    

    对于Angular 2中的ES6功能,您不需要键入 . 只需使用typescript 2.0或更高版本,并使用npm安装@ types / core-js:

    npm install --save-dev @types/core-js
    

    然后,将 TypeRootsTypes 属性添加到tsconfig.json:

    {
      "compilerOptions": {
        "target": "es5",
        "module": "es6",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "typeRoots": [
          "../node_modules/@types"
        ],
        "types" : [
          "core-js"
        ]
      },
      "exclude": [
        "node_modules"
      ]
    }
    

    这比使用Typings容易得多,如其他答案中所述 . 有关详细信息,请参阅Microsoft的博客文章:Typescript: The Future of Declaration Files

  • 48

    对于那些遵循 angular.io 上的Angular2教程的人来说,只是为了明确,这里是对mvdluit的答案的扩展,确切地说放置代码的位置:

    你的 main.ts 应该是这样的:

    /// <reference path="../node_modules/angular2/typings/browser.d.ts" />
    
    import {bootstrap} from 'angular2/platform/browser'
    import {AppComponent} from './app.component'
    // Add all operators to Observable
    import 'rxjs/Rx'
    
    bootstrap(AppComponent);
    

    请注意,您留下 /// 正斜杠,不要删除它们 .

    参考:https://github.com/ericmdantas/angular2-typescript-todo/blob/master/index.ts#L1

  • 48

    您可以在.ts文件的开头添加代码 .

    /// <reference path="../typings/index.d.ts" />
    
  • 3

    2.0.0-beta.6 (2016-02-11)的变更日志中提到了一种解决方法(在重大变化下列出):

    如果使用--target = es5,则需要在应用程序的某处添加一行(例如,在您调用bootstrap的.ts文件的顶部):

    ///<reference path="node_modules/angular2/typings/browser.d.ts"/>
    

    (请注意,如果您的文件与node_modules不在同一目录中,则需要在该路径的开头添加一个或多个../ . )

    确保你有正确的参考路径,我需要在开始时添加 ../ 以使其正常工作 .

相关问题