In an Angular app, I have seen that @Component
has property moduleId
. What does it mean?
And when module.id
is not defined anywhere, the app still works. How can it still work?
@Component({
moduleId: module.id,
selector: 'ng-app',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css'],
directives: [AppComponent]
});
The beta release of Angular (since vesion 2-alpha.51) supports relative assets for components, like templateUrl and styleUrls in the
@Component
decorator.module.id
works when using CommonJS. You don't need to worry about how it works.Source from Justin Schwartzenberger's post, thanks to @Pradeep Jain
Update on 16 Sep 2016:
Looks like if your using "module": "es6" in tsconfig.json, you dont have to use this :)
If you get
typescript error
, justdeclare
the variable in your file.UPDATE - December 08, 2016
The
module
keyword is available onnode
. So if you install@types/node
, in your project, you will havemodule
keyword automatically available in your typescript files without needing todeclare
it.npm install -D @types/node
Depending on your configuration, you might have to include this in your
tsconfig.json
file to get the tooling:Good Luck
This moduleId value is used by the Angular reflection processes and the metadata_resolver component to evaluate the fully-qualified component path before the component is constructed.
Adding
moduleId: module.id
fixes several issues that can occur when building native-angular apps and angular web apps. It's best practice to use it.It also enables the component to look in the current directory for files instead of from the top folder
Update for (2017-03-13):
Source: https://angular.io/docs/ts/latest/guide/change-log.html
Definition:
moduleId
parameter inside the@Component
annotation takes astring
value which is;"The module id of the module that contains the component."
CommonJS usage:
module.id
,SystemJS usage:
__moduleName
Reason to use
moduleId
:moduleId
is used to resolve relative paths for your stylesheets and templates as it says in the documentation.ref(old): https://angular.io/docs/js/latest/api/core/index/ComponentMetadata-class.html
ref: https://angular.io/docs/ts/latest/cookbook/component-relative-paths.html
Example usage:
Folder structure:
Without module.id:
With module.id:
tsconfig.json: