加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:41:41 所属栏目:安全 来源:网络整理
导读:我已经定义了以下Angular2组件: import {Component} from 'angular2/core';@Component({ selector: 'my-app',moduleId: module.id,templateUrl: './app.component.html'})export class AppComponent {} 当我尝试编译这个,我得到以下错误在第5行: src/app/
我已经定义了以下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模块变量(见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错误?

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

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

typings install dt~node --global --save

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

"globalDependencies": {
  "node": "registry:dt/node#6.0.0+20160608110640"
}

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

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

或者在项目中安装:

npm install @types/node --save

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读