【发布时间】:2016-08-10 14:05:42
【问题描述】:
我已经定义了以下 Angular2 组件:
import {Component} from 'angular2/core';
@Component({
selector: 'my-app',
moduleId: module.id,
templateUrl: './app.component.html'
})
export class AppComponent {
}
当我尝试编译它时,我在第 5 行收到以下错误:
src/app/app.component.ts(5,13): error TS2304: Cannot find name 'module'.
我相信 module.id 指的是 CommonJS module 变量(参见 here)。我在 tsconfig.json 中指定了 CommonJS 模块系统:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"pretty": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitUseStrict": false,
"noFallthroughCasesInSwitch": true
},
"exclude": [
"node_modules",
"dist",
"typings/browser.d.ts",
"typings/browser",
"src"
],
"compileOnSave": false
}
如何纠正 TypeScript 错误?
【问题讨论】:
-
我不认为 module 是在编译时提供给你的东西。
-
它在this project 中为我正确编译。不知道怎么样!!!所以我试图在一个更简单的项目中隔离它,但它在那里不起作用。所以试图弄清楚是什么使它在第一个项目中起作用。
-
您可以声明一个
module.d.ts文件,其内容为:declare var module: any;。然后从您的引导程序中引用此文件/// <reference path="path/to/typings/module.d.ts" /> -
在 typings.json 环境依赖项中:节点
-
@yurzui,你把它钉在了头上!你能把它写出来作为答案,以便我标记它是正确的吗?
标签: typescript angular commonjs