Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 480
Nirali Avatar asked Jun 14 '19 09:06

Nirali


People also ask

What command will create a new Angular app with a route routing module?

Run ng generate to create the application routing module.

How to create module with routing in Angular application?

After creating successfully app, we need to create module using angular cli command. angular provide command to create module with routing in angular application. so let's run bellow command to create admin module: run successfully command, it will create files as like bellow path:

How to create Admin module in angular 9/8?

After creating successfully app, we need to create module using angular cli command. angular provide command to create module with routing in angular application. so let's run bellow command to create admin module: run successfully command, it will create files as like bellow path: Read Also: How to Create Custom Pipe in Angular 9/8?

How do I add a route to a ngmodule?

The declaring NgModule. The name of the project. The route path for a lazy-loaded module. When supplied, creates a component in the new module, and adds the route to that component in the Routes array declared in the module provided in the --module option.

What is the difference between Route and component in angular?

Where path is the URL segment and component is the component to be loaded. This route tells angular to render HomeComponent when the user navigates to the URL “/home” Next, we register the routes with the RouterModule in our AppModule as shown below


3 Answers

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

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

like image 151
How To Developer Avatar answered Oct 17 '22 01:10

How To Developer


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
like image 31
nash11 Avatar answered Oct 17 '22 01:10

nash11


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
like image 11
Kiran Mali Avatar answered Oct 17 '22 03:10

Kiran Mali