admin管理员组

文章数量:1327945

I'm using angular-cli for running my typescript powered angular2 app. I have an AppComponent defined like this:

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

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

}

angular-cli can't pile this file, because it plains with an error mentioned in topic of this question in the line moduleId: module.id.

My tsconfig.json file is defined as below:

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

I'm using angular-cli for running my typescript powered angular2 app. I have an AppComponent defined like this:

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

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

}

angular-cli can't pile this file, because it plains with an error mentioned in topic of this question in the line moduleId: module.id.

My tsconfig.json file is defined as below:

{
    "pileOnSave": false,
    "pilerOptions": {
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "mapRoot": "",
        "module": "monjs",
        "moduleResolution": "node",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../dist/",
        "rootDir": ".",
        "sourceMap": true,
        "target": "es5",
        "inlineSources": true
    },
    "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
    ]
}
Share Improve this question edited Jun 12, 2017 at 1:30 Francesco Borzi 62.1k56 gold badges197 silver badges276 bronze badges asked May 11, 2016 at 14:20 Marek M.Marek M. 3,9519 gold badges52 silver badges104 bronze badges 1
  • you don't need moduleId: module.id when using angular-cli – maxime1992 Commented Nov 7, 2016 at 13:38
Add a ment  | 

4 Answers 4

Reset to default 1

To be able module.id, your project must be a monjs module, i.e. the module attribute set to monjs in the tsconfig.json file. Is it the case:

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

A possible work-around could be to add a app.d.ts file like this

https://github./johnpapa/pbp-a2-ward/blob/010523471e70677ba2493eb615c8e6316e3d4ee8/app/app.d.ts

Update mid 2017

Since angular migrated to using webpack there is no longer a need to declare moduleId as webpack plugins auto handle (add it) module.id in the final bundle.

As of 13.03.2017 Angular has removed all references to moduleId as it is now deprecated due to the inclusion of webpack.

We added a new SystemJS plugin (systemjs-angular-loader.js) to our remended SystemJS configuration. This plugin dynamically converts "ponent-relative" paths in templateUrl and styleUrls to "absolute paths" for you.

We strongly encourage you to only write ponent-relative paths. That is the only form of URL discussed in these docs. You no longer need to write @Component({ moduleId: module.id }), nor should you.

I had the same problem today when creating a first ponent in a feature folder using the angular-cli. As Maxime said, you can do this without a module id. But you have to use a relative path for your template and your styles, like inside the initially generated app.ponent:

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

本文标签: javascriptCannot find name module in Angular2 appStack Overflow