简体   繁体   English

TypeScript 和 ng2 rc.1 获取错误:(20, 15) TS2304:找不到名称“模块”

[英]TypeScript and ng2 rc.1 getting Error:(20, 15) TS2304: Cannot find name 'module'

TypeScript and ng2 rc.1 getting Error:(20, 15) TS2304: Cannot find name 'module'. TypeScript 和 ng2 rc.1 出现错误:(20, 15) TS2304:找不到名称“模块”。

when trying to use directive of module as in尝试使用模块指令时

@Component({
    selector: 'Notes1',
    moduleId: module.id,
    directives: [ModalDialog, FORM_DIRECTIVES, DisplayError],
    templateUrl: 'Notes1.html',
    styleUrls: ['Notes1.css']
})

any idea how to fix the TS error.. all is working fine at runtime知道如何修复 TS 错误.. 运行时一切正常

regards问候

Sean肖恩

The error means TypeScript compiler doesn't know what module is.该错误意味着 TypeScript 编译器不知道module是什么。 For a quick fix:快速修复:

declare var require: any;

To make it more complete, use a definition from DefinitelyTyped/node :为了使它更完整,使用来自绝对类型/节点的定义:

interface NodeRequireFunction {
    (id: string): any;
}

interface NodeRequire extends NodeRequireFunction {
    resolve(id:string): string;
    cache: any;
    extensions: any;
    main: any;
}

declare var require: NodeRequire;

interface NodeModule {
    exports: any;
    require: NodeRequireFunction;
    id: string;
    filename: string;
    loaded: boolean;
    parent: any;
    children: any[];
}

declare var module: NodeModule;

The way I fixed this was to simply add Id after module like this "moduleId": "commonjs", in my tsconfig file and the error went away!我解决这个问题的方法是在我的 tsconfig 文件中简单地在模块之后添加 Id,就像这样“moduleId”:“commonjs”,错误就消失了!

Hope this helps, Jim希望这会有所帮助,吉姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM