【问题标题】:Cannot find name 'module'. when using commonjs找不到名称“模块”。使用commonjs时
【发布时间】:2016-10-12 17:02:40
【问题描述】:

我有以下代码...

@Component({
    module: module.id, 
    selector: 'hero',
    templateUrl:'hero.component.html',
    styleUrls: ['hero.component.css'],
    directives: [HeroDetailComponent, MD_CARD_DIRECTIVES, MD_BUTTON_DIRECTIVES, MD_LIST_DIRECTIVES, MD_ICON_DIRECTIVES, MdToolbar, MD_INPUT_DIRECTIVES],
    providers: [HeroService],
    viewProviders: [MdIconRegistry]
})
export class HeroComponent implements OnInit{
...
}

//tsconfig.js
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "rootDir": "src/ng2",
    "moduleResolution": "node",
    "sourceMap": false,
    "inlineSources": true,
    "inlineSourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "outDir": "public/ng2",
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

但是当我跑步时,我得到...

src/ng2/components/hero/hero.component.ts(15,13):错误 TS2304:找不到名称“模块”。

更新

我尝试按照this question做步骤

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

但我仍然看到...

对象字面量只能指定已知属性,类型中不存在“模块”

UPDATE 2 将引导文件更新为如下所示...

import {bootstrap}     from '@angular/platform-browser-dynamic';
import {HeroComponent} from './components/hero/hero.component';
import { HTTP_PROVIDERS } from '@angular/http';
declare var module:any;
bootstrap(HeroComponent, [ HTTP_PROVIDERS ]);

我还看到了

src/ng2/components/hero/hero.component.ts(15,5): error TS2345: Argument of type '{ module: string;选择器:字符串;模板:字符串; styleUrls: 字符串[];指令:(typeof He...' 不可分配给类型为 '{ selector?: string; inputs?: string[]; outputs?: string[]; properties?: string[]; events?: strin.. .'. 对象字面量只能指定已知属性,并且 'module' 不存在于 type '{ selector?: string;输入?:字符串[];输出?:字符串[];属性?:字符串[];事件?:字符串...'。

这里是project

【问题讨论】:

    标签: typescript


    【解决方案1】:

    更新

    好的,让我们将该行放入打字稿定义文件中,并在引导文件中添加对该文件的引用。在 Angular 启动脚本的顶部,添加:

    ///<reference path="./path/to/custom.typings.d.ts"/>

    然后您需要在引用的路径上创建一个文件。在该文件中,添加以下行:declare var module:any;

    原答案

    在您的 Angular 启动脚本(调用 Angular 的 bootstrap 函数的脚本)中,在引导程序上方附近添加以下行:

    declare var module:any;

    我不确定为什么会发生此错误,因为我像您一样按照建议配置了我的依赖项。因为我知道这不会造成任何问题,所以我只是添加了一行让编译器停止抱怨

    【讨论】:

    • 试过了,但我似乎仍然有问题。有任何想法吗?见更新
    • 查看我编辑的答案。也许该行必须在打字稿定义文件中。
    • 我会试试看,我还能用 systemJS 作为我的转译器吗?
    猜你喜欢
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2020-11-18
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多