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.: ng g module newModule
ng 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?
According to Angular docs the way to create a component for specific module is,
"directory name" = where the CLI generated the feature module
Example :-
This generates a folder for the new component within the customer-dashboard folder and updates the feature module with the CustomerDashboardComponent
I didn't find an answer that showed how to use the cli to generate a component inside a top level module folder, and also have the component automatically added the the module's declaration collection.
To create the module run this:
To create the component inside the foo module folder and have it added to the foo.module.ts's declaration collection run this:
And the cli will scaffold out the module and component like this:
--EDIT the new version of the angular cli behaves differently. 1.5.5 doesn't want a module file name so the command with v1.5.5 should be
this will generate a module called
media
insidemodules
folder.the first part of the command
ng g c modules/media/picPick
will generate a component folder calledpicPick
insidemodules/media
folder witch contain our newmedia
module.the second part will make our new
picPick
component declared inmedia
module by importing it in the module file and appending it todeclarations
array of this module.I ran into this issue today while scaffolding an Angular 9 application. I got the "module does not exist error" whenever I added the
.module.ts
or.module
to the module name. The cli only needs the name of the module with no extension. Assuming I had a module name:brands.module.ts
, the command I used wasremove the
--dry-run
once you've confirmed the file structure is correct.If you have multiple apps declared in .angular-cli.json ( e.g. in case working on feature module)
You can :
-a stands for --app (name)