【发布时间】:2016-03-22 15:20:40
【问题描述】:
我在 Visual Studio 2015 Update 1 中试用 angular2 beta 版本时遇到了这个错误Cannot find module 'angular2/core' .
代码如下:
import {Component} from "angular2/core"
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent {
}
我的 Visual Studio ASP.NET 5 项目中有 angular2 beta 版本。我在构建项目时也启用了编译 TS 文件。
我尝试了一些方法,但都没有成功:
- 我在 ts 文件的顶部添加了
/// <reference path="../node_modules/angular2/core.d.ts" />。
2.
import {Component} from "angular2/bundles/angular2"
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent {
}
3.
import {Component} from "../node_modules/angular2/core"
@Component({
selector: 'my-app',
template: "<h1>My First Angular 2 App</h1>"
})
export class AppComponent {
}
更新:
这是我的 tsconfig.json
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": true,
"sourceMap": true,
"target": "es6",
"module": "system",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules",
"wwwroot",
"bower_components"
]
}
更新:
我将 "moduleResolution": "node" 添加到 tsconfig.json 文件中,但现在又出现一组错误:
1>----- 重建所有开始:项目:Terminal.Web,配置: 调试任何 CPU ------ 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(6,14): 错误 TS2300:构建:重复标识符“PropertyKey”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(9,5): 错误 TS2300:构建:重复标识符“完成”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(10,5): 错误 TS2300:构建:重复标识符“值”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(248,5): 错误 TS2300:构建:重复标识符“EPSILON”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(283,5): 错误 TS2300:构建:重复标识符“MAX_SAFE_INTEGER”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(290,5): 错误 TS2300:构建:重复标识符“MIN_SAFE_INTEGER”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(346,5): 错误 TS2300:构建:重复的标识符“标志”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(498,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(561,5): 错误 TS2300:构建:重复标识符“大小”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(570,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(581,5): 错误 TS2300:构建:重复标识符“大小”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(590,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(605,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\Test\node_modules\angular2\typings\es6-shim\es6-shim.d.ts(619,5): 错误 TS2300:构建:重复标识符“原型”。 1>C:\程序 文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(3841,14):错误 TS2300:构建:重复标识符“PropertyKey”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4061,5):错误 TS2300:构建:重复标识符“EPSILON”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4096,5):错误 TS2300:构建:重复标识符“MAX_SAFE_INTEGER”。 1>C:\程序 文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4103,5):错误 TS2300:构建:重复标识符“MIN_SAFE_INTEGER”。 1>C:\程序 文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4389,5):错误 TS2300:构建:重复标识符“完成”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4390,5):错误 TS2300:构建:重复的标识符“值”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4619,5):错误 TS2300:构建:重复的标识符“标志”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4647,5):错误 TS2300:构建:重复标识符“大小”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4657,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4674,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4686,5):错误 TS2300:构建:重复标识符“大小”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4696,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(4712,5):错误 TS2300:构建:重复标识符“原型”。 1>C:\程序文件 (x86)\Microsoft SDKs\TypeScript\1.7\lib.es6.d.ts(5099,5):错误 TS2300:构建:重复标识符“原型”。 ==========全部重建:0成功,1失败,0跳过==========
【问题讨论】:
-
尝试在您的 tsconfig 中添加
"moduleResolution" : "node"。 -
我编译了相同的示例,但使用了 Code 和 npm。它工作正常,您可以发布您的 tsconfig.json 文件吗?
-
检查您在
index.html中导入 js 文件的顺序我遇到了同样的问题,但是在阅读了这个 repo github.com/pkozlowski-opensource/ng2-play/blob/master/… 后我已经解决了我的问题。希望它能帮助您找到错误 -
第一:如果你还没有集成 system.import 那么你将如何设置引导文件(主组件)的入口点,第二:检查你的 node_modules 包中的 Core、common 等模块angular2 文件夹。
-
这个answer 的另一个stackoverflow 问题解决了这个问题。
标签: typescript angular