typescript 构建:找不到名称 Promise - Visual Studio 2015 w/MVC6 和 Angular 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36178790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:22:18  来源:igfitidea点击:

Build: Cannot find name Promise - Visual Studio 2015 w/ MVC6 and Angular 2

typescriptvisual-studio-2015angularasp.net-core-mvc

提问by Jose A

First of all: I've checked these:

首先:我检查了这些:

https://github.com/angular/angular/issues/7052

https://github.com/angular/angular/issues/7052

https://github.com/angular/angular/issues/4902

https://github.com/angular/angular/issues/4902

Typescript cannot find name 'Promise' despite using ECMAScript 6

尽管使用 ECMAScript 6,Typescript 还是找不到名称“Promise”

how to use es6-promises with typescript?

如何在打字稿中使用 es6-promises?

Visual Studio Code: Cannot find name angular?

Visual Studio Code:找不到名称角度?

How to get rid of Angular2 / TypeScript errors on Cannot find map, Promise ... when targeting -es5

如何摆脱 Angular2/TypeScript 错误无法找到地图,Promise ... 当定位 -es5 时

And many many many more. Been banging my head for 2 days now.

还有很多很多。已经敲我的头 2 天了。

And yes:

是的:

I have referenced in my boot.ts file (or bootstrap file):

我在 boot.ts 文件(或 bootstrap 文件)中引用了:

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

Now, to the problem: I'm using Visual Studio 2015 W/ Update 1 and an ASP.NET MVC 6 project (Release Candidate 1) + Typescript 1.8.1.

现在,问题来了:我使用的是 Visual Studio 2015 W/Update 1 和一个 ASP.NET MVC 6 项目(Release Candidate 1)+ Typescript 1.8.1。

I used this tutorial to do the setup: http://www.mithunvp.com/angular-2-in-asp-net-5-typescript-visual-studio-2015/

我使用本教程进行设置:http: //www.mithunvp.com/angular-2-in-asp-net-5-typescript-visual-studio-2015/

I've been using Angular 2 successfully for a while, and now THE SAME CODE (afaik) will not compile. It will give me the "Cannot find name: Promise" in browser.d.ts file", located in node_modules/angular2/platform:

我已经成功使用 Angular 2 一段时间了,现在相同的代码 (afaik) 无法编译。它会给我“在 browser.d.ts 文件中找不到名称:Promise”,位于 node_modules/angular2/platform:

tsconfig.json

配置文件

 {
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../wwwroot/appScripts/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules"
  ]
}

package.json

包.json

{
    "version": "1.0.0",
    "name": "asp.net", 
  "private": true,
  "dependencies": {
    "angular2": "2.0.0-beta.11",
    "systemjs": "0.19.24",
    "es6-promise": "^3.1.2",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.6.4"
  },
  "devDependencies": {
    "gulp": "^3.9.1"
  }
}

gulpfile.js

gulpfile.js

/*
This file in the main entry point for defining Gulp tasks and using Gulp plugins.
Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/

/// <binding AfterBuild='moveToLibs' />

var gulp = require('gulp');

gulp.task('default', function () {
    // place code for your default task here
});

gulp.task('moveToLibs', function () {
    gulp.src([
      'node_modules/angular2/bundles/js',
      'node_modules/angular2/bundles/angular2.*.js*',
      'node_modules/angular2/bundles/angular2-polyfills.js',
      'node_modules/angular2/bundles/http.*.js*',
      'node_modules/angular2/bundles/router.*.js*',
      'node_modules/es6-shim/es6-shim.min.js*',
      'node_modules/angular2/es6/dev/src/testing/shims_for_IE.js',
      'node_modules/systemjs/dist/*.*',
      'node_modules/jquery/dist/jquery.*js',
      'node_modules/bootstrap/dist/js/bootstrap*.js',
      'node_modules/rxjs/bundles/Rx.js'
    ]).pipe(gulp.dest('./wwwroot/libs/'));
});

I've even tried using the reference path inside the node_module/angular2/platform/browser.d.ts file, and it threw an error for using reference path in non-module files.

我什至尝试使用 node_module/angular2/platform/browser.d.ts 文件中的引用路径,但在非模块文件中使用引用路径时抛出错误。

The only thing is somewhat working (I didn't implement it fully, because it is insane):Copy and paste the code inside es6-promise.d.ts file in all the files that are asking it.

唯一的事情有点工作(我没有完全实现它,因为它很疯狂):复制并粘贴es6-promise.d.ts文件中的所有请求它的文件中的代码。

EditSniffSniffSomething is smelling bad in here: I decided to rename the node_module folder into _bak_node_module_folder and reinstalled all the packages:

编辑Sniff Sniff这里有些东西很糟糕:我决定将 node_module 文件夹重命名为 _bak_node_module_folder 并重新安装所有软件包:

    Severity    Code    Description Project File    Line    Suppression State
Error   MSB4018 The "FindConfigFiles" task failed unexpectedly.
System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
   at System.IO.Path.GetDirectoryName(String path)
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.EnumerateFiles(String path)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherFiles(String directoryPath, String projectPath, Func`2 filter)
   at TypeScript.Tasks.FileHelpers.RecursiveGatherConfigFiles(String directoryPath, String projectPath)
   at TypeScript.Tasks.FindConfigFiles.FindConfigFilesOnDisk()
   at TypeScript.Tasks.FindConfigFiles.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() Angular2    C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TypeScript\Microsoft.TypeScript.targets 153 

I really really hate this character limitation with Windows. If this is the problem, I think I'll jump myself from the tallest bridge in my city.

我真的很讨厌 Windows 的这种字符限制。如果这是问题所在,我想我会从我所在城市最高的桥上跳下来。

Edit x2So, I decided to update Visual Studio's NPM as suggested and it didn't work out. So I decided to grab Nodejs and run it from the cmd. Unfortunately the problem still persists:

编辑 x2所以,我决定按照建议更新 Visual Studio 的 NPM,但没有成功。所以我决定获取 Nodejs 并从 cmd 运行它。不幸的是,问题仍然存在:

Here is the output:

这是输出:

[0] node_modules/angular2/typings/browser.d.ts(6,14): error TS2300: Duplicate identifier 'PromiseConstructor'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(22,5): error TS2300: Duplicate identifier 'done'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(23,5): error TS2300: Duplicate identifier 'value'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(46,5): error TS2300: Duplicate identifier 'size'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(52,5): error TS2300: Duplicate identifier 'prototype'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(66,5): error TS2300: Duplicate identifier 'size'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(72,5): error TS2300: Duplicate identifier 'prototype'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(88,5): error TS2300: Duplicate identifier 'prototype'.
[0] node_modules/angular2/typings/es6-collections/es6-collections.d.ts(103,5): error TS2300: Duplicate identifier 'prototype'.
[0] node_modules/angular2/typings/es6-promise/es6-promise.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.
[0] node_modules/angular2/typings/es6-promise/es6-promise.d.ts(42,16): error TS2300: Duplicate identifier 'Promise'.
[0] typings/browser/ambient/es6-shim/index.d.ts(11,5): error TS2300: Duplicate identifier 'done'.
[0] typings/browser/ambient/es6-shim/index.d.ts(12,5): error TS2300: Duplicate identifier 'value'.
[0] typings/browser/ambient/es6-shim/index.d.ts(477,11): error TS2300: Duplicate identifier 'Promise'.
[0] typings/browser/ambient/es6-shim/index.d.ts(496,11): error TS2300: Duplicate identifier 'PromiseConstructor'.
[0] typings/browser/ambient/es6-shim/index.d.ts(554,13): error TS2300: Duplicate identifier 'Promise'.
[0] typings/browser/ambient/es6-shim/index.d.ts(563,5): error TS2300: Duplicate identifier 'size'.
[0] typings/browser/ambient/es6-shim/index.d.ts(572,5): error TS2300: Duplicate identifier 'prototype'.
[0] typings/browser/ambient/es6-shim/index.d.ts(583,5): error TS2300: Duplicate identifier 'size'.
[0] typings/browser/ambient/es6-shim/index.d.ts(592,5): error TS2300: Duplicate identifier 'prototype'.
[0] typings/browser/ambient/es6-shim/index.d.ts(607,5): error TS2300: Duplicate identifier 'prototype'.
[0] typings/browser/ambient/es6-shim/index.d.ts(621,5): error TS2300: Duplicate identifier 'prototype'.
[0] 2:47:31 PM - Compilation complete. Watching for file changes.

回答by tomitrescak

The REASON for this is that you are targeting ES5, which does not have Promises. If you change to ES6 the error will magically disappear.

原因是您的目标是 ES5,它没有 Promises。如果您更改为 ES6,错误将神奇地消失。

If you do need to target ES5, just use the es6-shim mentioned with previous answers.

如果您确实需要针对 ES5,只需使用前面答案中提到的 es6-shim。

回答by Mcanic

This solved it for me, using Angular 2.0.0-rc.1: In the class where you're bootstrapping Angular, put this line on top:

这为我解决了这个问题,使用 Angular 2.0.0-rc.1:在你引导 Angular 的类中,把这一行放在最上面:

/// <reference path="../typings/browser/ambient/es6-shim/index.d.ts"/>

回答by Andre L

I just ran into this exact same issue after updating es-promise from 3.0.2 to 3.1.2. I ultimately ended up installing the newer version of nodejs with a newer version of npm and then deleted my node_module folder and reinstalled the packages. I then added the reference path to the main.ts or boot.ts (whatever you named it) file. That fixed it.

在将 es-promise 从 3.0.2 更新到 3.1.2 后,我刚刚遇到了这个完全相同的问题。我最终最终使用较新版本的 npm 安装了较新版本的 nodejs,然后删除了我的 node_module 文件夹并重新安装了软件包。然后我将引用路径添加到 main.ts 或 boot.ts(无论您命名它)文件。那解决了它。

Just like Steve said.

就像史蒂夫说的那样。

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

回答by Karl P

I was running into the same problem for a long time in Visual Studio. I noticed that if you do not wait for ALL of your npm Dependencies to restore BEFORE you try to build, you put your project into a world of hurt.

我在 Visual Studio 中遇到了同样的问题很长时间。我注意到,如果您在尝试构建之前不等待所有 npm 依赖项恢复,那么您的项目就会陷入困境。

My advice, go back to the original dependency versions in your package.json file from the demo project on mithunvp.com. Wait for the dependencies to restore, then rebuild your VS project.

我的建议是,从 mithunvp.com 上的演示项目返回到 package.json 文件中的原始依赖版本。等待依赖项恢复,然后重建您的 VS 项目。

For the character limit issue, you must update npm within Visual Studio.
See: Upgrade npm on Windows

对于字符限制问题,您必须在 Visual Studio 中更新 npm。
请参阅:在 Windows 上升级 npm

回答by Darkseal

This ES6 type definitions issue has been a nightmare for me today: I ran through a lot of SO answers that tried to work around the issue in a number of ways, including:

这个 ES6 类型定义问题今天对我来说是一场噩梦:我浏览了很多 SO 答案,试图以多种方式解决这个问题,包括:

  • Add one or more "triple-slash" references to the bootstrap TS file, which is something I really don't want to do, expecially now that browser.d.ts is not included anymore by Angular2 most recent RCs.
  • Install typingsand/or download es6-shim.d.ts manually, which is also something I would avoid because I'm looking for a solution that doesn't force me to go outside the VS2015 GUI.
  • 向 bootstrap TS 文件添加一个或多个“三重斜线”引用,这是我真的不想做的事情,特别是现在 Angular2 最新的 RC 不再包含 browser.d.ts。
  • typings手动安装和/或下载 es6-shim.d.ts,这也是我会避免的事情,因为我正在寻找一种不会强迫我离开 VS2015 GUI 的解决方案。

After a lot of research I came out with this workaroundwhich seems to fix the issue for good with few simple steps:

经过大量研究,我提出了这个解决方法,它似乎通过几个简单的步骤就可以很好地解决问题:

  • add typingsin the project's package.jsonfile.
  • add a scriptblock in that very same package.jsonfile to execute/update typings after each NPMaction.
  • add a typings.jsonfile in the project's root folder containing a reference to core-js(overall better than es6-shimatm).
  • typings在项目的package.json文件中添加。
  • script在同一个package.json文件中添加一个块以在每个NPM操作后执行/更新类型。
  • typings.json在项目的根文件夹中添加一个文件,其中包含对core-js(总体上比es6-shimatm更好)的引用。

That's it.

而已。

You can also take a look to this poston my blog for additional details regarding the issue.

您还可以查看我博客上的这篇文章,了解有关该问题的更多详细信息。

回答by Bit Monkey

Upate for VS2015 with Angular2 RC5:

使用 Angular2 RC5 更新 VS2015

(This answer assumes you are generally following the angular2 Quick Start at angular.io and trying to get it to work in a VS2015 project.)

(此答案假设您通常遵循 angular.io 上的 angular2 快速入门并尝试使其在 VS2015 项目中工作。)

Add the following line to the top of your main.ts file (adjust path according to your project's actual file structure):

将以下行添加到 main.ts 文件的顶部(根据项目的实际文件结构调整路径):

/// < reference path="../typings/globals/core-js/index.d.ts" />

/// < 参考路径="../typings/globals/core-js/index.d.ts" />

The typings file referenced above gets imported automatically upon build based on the contents of the typings.json file.

上面引用的typings 文件在构建时根据typings.json 文件的内容自动导入。

You may also need to add a line to your .csproj project file to get the project to compile (VS2015 update 3 currently ignores the tsconfig.json line for "experimentalDecorators"). To do this, first unload your project by right-clicking on your project in the solution explorer window and clicking 'unload project'. Open your .csproj file and look for a < PropertyGroup> node containing other < Typescript...> elements. Add an element for TypeScript experimental decorators at the bottom before the closing tag like this:

您可能还需要在 .csproj 项目文件中添加一行以编译项目(VS2015 更新 3 当前忽略“experimentalDecorators”的 tsconfig.json 行)。为此,首先通过在解决方案资源管理器窗口中右键单击您的项目并单击“卸载项目”来卸载您的项目。打开您的 .csproj 文件并查找包含其他 < Typescript...> 元素的 < PropertyGroup> 节点。在底部的结束标记之前为 TypeScript 实验性装饰器添加一个元素,如下所示:

< PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

< PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

...

...

< TypeScriptExperimentalDecorators>true< /TypeScriptExperimentalDecorators>

< TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators>

< /PropertyGroup>

< /属性组>

Load your project and rebuild. It should compile now without error. If you still have issues, make sure you have the latest version of TypeScript installed (at least 1.8.x). In Visual Studio, go to Tools > Extensions and Updates... and search for TypeScript. Install the "TypeScript 1.8.x for Visual Studio 2015" extension. Also available at https://www.microsoft.com/en-us/download/details.aspx?id=48593.

加载您的项目并重建。它现在应该编译没有错误。如果仍有问题,请确保安装了最新版本的 TypeScript(至少 1.8.x)。在 Visual Studio 中,转到“工具”>“扩展和更新...”并搜索 TypeScript。安装“TypeScript 1.8.x for Visual Studio 2015”扩展。也可从https://www.microsoft.com/en-us/download/details.aspx?id=48593 获得

If you have nodejs installed, you can install TypeScript from the command line by entering the command: "npm install -g typescript". Check your installed TypeScript version from a Node.js command window with the command: "tsc -v". Running this command from a standard command window will likely only show "Version 1.0.3.0" instead of the expected Version 1.8.x. (For more information, see how to synchronize Node.js with VS2015 at http://ryanhayes.net/synchronize-node-js-install-version-with-visual-studio-2015/)

如果你安装了 nodejs,你可以从命令行输入以下命令安装 TypeScript:“npm install -g typescript”。从 Node.js 命令窗口使用以下命令检查您安装的 TypeScript 版本:“tsc -v”。从标准命令窗口运行此命令可能只会显示“1.0.3.0 版”而不是预期的 1.8.x 版。(有关更多信息,请参阅如何在http://ryanhayes.net/synchronize-node-js-install-version-with-visual-studio-2015/上同步 Node.js 与 VS2015 )

Hope this helps someone.

希望这可以帮助某人。

回答by July.Tech

This is what fixed it for me:

这就是为我修复它的原因:

///<reference path="./../typings/globals/core-js/index.d.ts"/>

on top of the main .ts file.

在主 .ts 文件之上。

回答by kimbaudi

I don't think solving this issue by adding ///<reference path""/>to your main.ts (or wherever you bootstrap Angular2) is the ideal solution. The issue your facing has to do with your placement of tsconfig.jsonand the gulp script` you're using to setup your Angular2 project. Before I can help out more, can you check the following?

我不认为通过添加///<reference path""/>到 main.ts(或引导 Angular2 的任何地方)来解决这个问题是理想的解决方案。您面临的问题与您tsconfig.json用来设置 Angular2 项目的 gulp 脚本的位置和 gulp 脚本有关。在我提供更多帮助之前,您可以检查以下内容吗?

  1. did you run the npm script to install typings (i.e., typings install) so that there is a typings folder in your project? What folders are in your typingsfolder? globals? browser?
  2. what is the contents of your gulp task to compile typescript? did you use gulp-typescriptor gulp-tscor something else?
  1. 您是否运行 npm 脚本来安装typings(即typings install),以便在您的项目中有一个typings 文件夹?您的typings文件夹中有哪些文件夹?globals? browser?
  2. 编译打字稿的 gulp 任务的内容是什么?你用过gulp-typescript还是gulp-tsc其他的?

The directory containing your tsconfig.jsonfile is considered the root of your TypeScript project. Therefore, any .ts/.d.ts files under your TypeScript project will try to be compiled (including files in your typingsfolder and/or www/libsfolder. I can see that you included an "exclude": ["node_modules"] to your tsconfig.json, so the 'Cannot find name Promise issue' is not coming from the node_modulesfolder (its most likely coming from your typingsfolder and/or www/libsfolder.

包含您的tsconfig.json文件的目录被视为您的 TypeScript 项目的根目录。因此,您的 TypeScript 项目下的任何 .ts/.d.ts 文件都将尝试编译(包括您的typings文件夹和/或www/libs文件夹中的文件。我可以看到您在 tsconfig 中包含了一个 "exclude": ["node_modules"] .json,所以“找不到名称承诺问题”不是来自node_modules文件夹(它很可能来自您的typings文件夹和/或www/libs文件夹。

I'm pretty sure that you resolve the Cannot find name Promiseissue without having to add ///<reference path""/>to the top of your bootstrap file (main.ts most likely) by adding an extra to your "exclude" in your typings.jsonor your gulptask in your gulpfile.js.

我很确定您Cannot find name Promise无需添加///<reference path""/>到引导程序文件的顶部(最有可能是 main.ts)就可以解决问题,方法是typings.jsongulpfile.js.

Don't get me wrong, adding ///<reference path"/>will solve your issue, but I don't think its the ideal solution.

不要误会我的意思,添加///<reference path"/>将解决您的问题,但我认为这不是理想的解决方案。

回答by Fayez Mutairi

I resolved this issue by downloading es6-shim.d.ts from DefinitelyTyped and reference it in boot.ts where I have angular2 bootstrap using this code

我通过从 absoluteTyped 下载 es6-shim.d.ts 并在 boot.ts 中引用它来解决这个问题,其中我有 angular2 bootstrap 使用此代码

/// <reference path="../typings/es6-shim/es6-shim.d.ts"/>

I have also removed this reference as it created conflicts.

我也删除了这个引用,因为它造成了冲突。

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

This resolve my issues and my files were compiled correctly with no errors.

这解决了我的问题,我的文件被正确编译,没有错误。

回答by Fayez Mutairi

Another solution is to add a reference to

另一种解决方案是添加对

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

in the top of the file where you bootstraping angular and make sure you add

在文件的顶部引导 angular 并确保添加

"moduleResolution": "node",

to "compilerOptions"of your tsconfig.jsonsomething like:

到您的tsconfig.json 的“compilerOptions”,例如:

{
   "compilerOptions": {
       "target": "es5",
       "module": "system",
       "moduleResolution": "node",
       "emitDecoratorMetadata": true,
       "experimentalDecorators": true,
   }
}