snippettypescriptangularCritical
Create component & add it to a specific module with Angular-CLI
Viewed 0 times
angularcreateclispecificwithmoduleaddcomponent
Problem
I'm starting to use angular-cli and I've already read a lot to find an answer about what I want to do...no success, so I came here.
Is there a way to create a component to a new module?
e.g.:
because the angular-cli default behavior is to put all new components inside
Is there a way to create a component to a new module?
e.g.:
ng g module newModuleng g component newComponent (how to add this component to newModule??)because the angular-cli default behavior is to put all new components inside
app.module. I would like to choose where my component will be, so that I can create separated modules and won't have all my components inside app.module . It is possible to do that using angular-cli or do I have to do this manually?Solution
To create a component as part of a module you should
UPDATE: Angular 9
Now it doesn't matter what folder you are in when generating the component.
Note: When the Angular CLI sees new-module/new-component, it understands and translates the case to match
ng g module newModuleto generate a module,
cd newModuleto change directory into thenewModulefolder
ng g component newComponentto create a component as a child of the module.
UPDATE: Angular 9
Now it doesn't matter what folder you are in when generating the component.
ng g module NewModuleto generate a module.
ng g component new-module/new-componentto create NewComponent.
Note: When the Angular CLI sees new-module/new-component, it understands and translates the case to match
new-module -> NewModule and new-component -> NewComponent. It can get confusing in the beginning, so easy way is to match the names in #2 with the folder names for the module and component.Context
Stack Overflow Q#40649799, score: 367
Revisions (0)
No revisions yet.