Generate module with component and routing in Angular 8 (using angular cli command)
Asked Answered
S

4

15

I have created new angular project using angular cli command ng new my-app

Then, I wanted to create module named landing-page inside src/app directory, so i used command ng g m landing-page --routing=true which creates two files i.e. one module and one its routing file as following :

src/app/landing-page/landing-page-routing.module.ts (254 bytes) 

src/app/landing-page/landing-page.module.ts (300 bytes)

but Now, instead of that i want to create module with its root component and routing both in single folder named as landing-page, so how can i do that with single cli command ?

Snooker answered 14/6, 2019 at 9:38 Comment(2)
The only existing solution is to create your own schematic command(angular.io/guide/schematics-authoring).Jerryjerrybuild
hmm ok, i am not aware of this and was expecting that there might be command to do so as asked in questionSnooker
E
27

There is no way to do this as of yet in one single command other than creating your own schematic command as mentioned in the comments because the module and component are two different schematics and as per the documentation, the <schematic> argument in ng g <schematic> [options] can only take one sub-command.

You can, however, combine two commands in one line using && and create a module and the component in the same folder.

ng g m landing-page --routing=true && ng g c landing-page --skip-tests=true -m=landing-page
Erythrite answered 14/6, 2019 at 10:28 Comment(0)
P
15

Use --module app.module which helps to update in app.module.

Below command creates a home module, a component with a routing module.

ng g m home --routing=true --module app.module && ng g c home

I would like to suggest you use lazy loading routing which helps in a big application for route lazy loading and app performance.

User below command to create a module, component, route and it will also update your app-routing.module.ts.

ng g m home --route home --module app.module
Pathos answered 10/6, 2020 at 12:30 Comment(0)
K
4

ng g m Header --route header --module app.module

this command create , parent module , parent component , and routing , try it .. tc

Kilovoltampere answered 30/1, 2020 at 4:30 Comment(0)
H
1

This command create module, component and routes in angular

1

ng g m admin --routing=true --module app.module && ng g c admin

Hombre answered 20/8, 2022 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.