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 componen
Read the description of --route
https://angular.io/cli/generate#module-command,
To archive such, you must add the route of that component-module to somewhere and specify the route name.
ng generate module component-name --module=any-parent-module --route=route-path
ng g m modules/media
this will generate a module called media
inside modules
folder.
ng g c modules/media/picPick --module=modules/media/media.module.ts
the first part of the command ng g c modules/media/picPick
will generate a component folder called picPick
inside modules/media
folder witch contain our new media
module.
the second part will make our new picPick
component declared in media
module by importing it in the module file and appending it to declarations
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 was
ng g c path/to/my/components/brands-component -m brands --dry-run
remove the --dry-run
once you've confirmed the file structure is correct.
Module
using ng generate module <module name>
.ng generate component <component name> --module=<module name>
A common pattern is to create a feature with a route, a lazy loaded module, and a component.
Route: myapp.com/feature
app-routing.module.ts
{ path: 'feature', loadChildren: () => import('./my-feature/my-feature.module').then(m => m.MyFeatureModule) },
File structure:
app
└───my-feature
│ │ my-feature-routing.module.ts
│ │ my-feature.component.html
│ │ my-feature.component.css
│ │ my-feature.component.spec.ts
│ │ my-feature.component.ts
│ │ my-feature.module.ts
This can all be done in the cli with:
ng generate module my-feature --module app.module --route feature
Or shorter
ng g m my-feature --module app.module --route feature
Or if you leave out the name the cli will prompt you for it. Very useful when you need to create several features
ng g m --module app.module --route feature
Basic:
ng g module chat
ng g service chat/chat -m chat
ng g component chat/chat-dialog -m chat
In chat.module.ts:
exports: [ChatDialogComponent],
providers: [ChatService]
In app.module.ts:
imports: [
BrowserModule,
ChatModule
]
Now in app.component.html:
<chat-dialog></chat-dialog>
LAZY LOADING:
ng g module pages --module app.module --route pages
CREATE src/app/pages/pages-routing.module.ts (340 bytes)
CREATE src/app/pages/pages.module.ts (342 bytes)
CREATE src/app/pages/pages.component.css (0 bytes)
CREATE src/app/pages/pages.component.html (20 bytes)
CREATE src/app/pages/pages.component.spec.ts (621 bytes)
CREATE src/app/pages/pages.component.ts (271 bytes)
UPDATE src/app/app-routing.module.ts (8611 bytes)
ng g module pages/forms --module pages/pages.module --route forms
CREATE src/app/forms/forms-routing.module.ts (340 bytes)
CREATE src/app/forms/forms.module.ts (342 bytes)
CREATE src/app/forms/forms.component.css (0 bytes)
CREATE src/app/forms/forms.component.html (20 bytes)
CREATE src/app/forms/forms.component.spec.ts (621 bytes)
CREATE src/app/forms/forms.component.ts (271 bytes)
UPDATE src/app/pages/pages-routing.module.ts (437 bytes)