簡體   English   中英

在Angular2應用中找不到名稱模塊

[英]Cannot find name module in Angular2 app

我正在使用angular-cli運行我的打字稿驅動的angular2應用程序。 我有一個像這樣定義的AppComponent

import { Component } from '@angular/core';
import { ServersListComponent } from './servers-list/servers-list.component';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html',
    styleUrls: ['app.component.css'],
    directives: []
})
export class AppComponent {

}

angular-cli無法編譯該文件,因為它在moduleId: module.id行中抱怨此問題的主題中提到的錯誤。

我的tsconfig.json文件定義如下:

{
    "compileOnSave": false,
    "compilerOptions": {
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "mapRoot": "",
        "module": "commonjs",
        "moduleResolution": "node",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../dist/",
        "rootDir": ".",
        "sourceMap": true,
        "target": "es5",
        "inlineSources": true
    },
    "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}

為了能夠module.id ,您的項目必須是commonjs模塊,即在tsconfig.json文件commonjs module屬性設置為commonjs 是這樣嗎?

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs", // <-----
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

2017年中更新

由於angular已遷移到使用webpack,因此不再需要將moduleId聲明為webpack插件自動處理(添加)最終包中的module.id

2017年3月13日起, Angular刪除了對moduleId所有引用,因為由於包含webpack而已棄用它。

我們在建議的SystemJS配置中添加了一個新的SystemJS插件(systemjs-angular-loader.js)。 該插件為您動態地將templateUrl和styleUrls中的“相對於組件”的路徑轉換為“絕對路徑”。

我們強烈建議您僅編寫組件相對路徑。 這是這些文檔中討論的URL的唯一形式。 您不再需要編寫@Component({moduleId:module.id}),也不需要。

今天,在使用angular-cli在特征文件夾中創建第一個組件時,我遇到了同樣的問題。 正如Maxime所說,您可以在沒有模塊ID的情況下執行此操作。 但是您必須為模板和樣式使用相對路徑,例如在最初生成的app.component內部:

templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM