在Angular2代码中的TypeScript错误:找不到名称’module’

前端之家收集整理的这篇文章主要介绍了在Angular2代码中的TypeScript错误:找不到名称’module’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经定义了以下Angular2组件:
  1. import {Component} from 'angular2/core';
  2.  
  3. @Component({
  4. selector: 'my-app',moduleId: module.id,templateUrl: './app.component.html'
  5. })
  6. export class AppComponent {
  7. }

当我尝试编译这个,我得到以下错误在第5行:

  1. src/app/app.component.ts(5,13): error TS2304: Cannot find name 'module'.

我相信module.id是指CommonJS模块变量(见here)。我在tsconfig.json中指定了CommonJS模块系统:

  1. {
  2. "compilerOptions": {
  3. "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
  4. },"exclude": [
  5. "node_modules","dist","typings/browser.d.ts","typings/browser","src"
  6. ],"compileOnSave": false
  7. }

如何纠正TypeScript错误

您需要安装节点ambientDependencies。 Typings.json
  1. "ambientDependencies": {
  2. "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#138ad74b9e8e6c08af7633964962835add4c91e2",

另一种方式可以使用typings管理器全局安装节点定义文件

  1. typings install dt~node --global --save

然后你的typings.json文件将如下所示:

  1. "globalDependencies": {
  2. "node": "registry:dt/node#6.0.0+20160608110640"
  3. }

如果使用Typescript 2 ^只需使用以下命令:

  1. npm install @types/node --global --save

或者在项目中安装:

  1. npm install @types/node --save

然后从引导中添加对它的引用:

  1. /// <reference path="../../node_modules/@types/node/index.d.ts" />

猜你在找的Angularjs相关文章