138

Is any way to create a routing.module.ts while generating a new module in angular-cli application other than creating it manually?

I recently started implementing lazy loading in my application and would like to combine both operations in one step.

13 Answers 13

241

There is no separate command to create routing.module file. But, that can be created while on the creation of the module:

ng generate module [module-name] --routing

or the shorthand version of the command:

ng g m [module-name] --routing

... will create the module and add the mappings/metadata linkings.


I was searching about this a bit and found some article which has a very good explanation for different kind of commands.

The Ultimate Angular CLI Reference

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Saiyaff Farouk, the above command creates both module.ts and routing.module.ts together at the same. Is it possible to generate routing.module.ts alone since I already have one module.ts .
@Dhana I don't think that can be possible with a command for the scenario of 'Is it possible to generate routing.module.ts alone since I already have one module.ts'. To overcome this you can try writing your own schematic. But, let me do a bit of a further research and see whether what you want is available already in any sorta forms
You can generate my-routing.module.ts for an existing my.module.ts with ng generate module my-routing --module my --flat. Then add RouterModule to the new routing module as shown in this answer.
28

Module with Routing Create CMD :-

ng g m [ModuleName] --routing

1 Comment

it's better to create only routing files in that you've forgotten while generate the module
24

Late but very useful.

ng g m about --module app --route about 

The above command will generate about module with about component and add lazy load route at app module for routing about route.

1 Comment

Indeed very helpful - although i faced "Couldn't find a route declaration in /src/app/app.module.ts" in the first place, when using your command like this. What finally helped was passing the full file name of the app's routing module: --module app.routing.ts. Got the hint from stackoverflow.com/a/69779475/12924116.
19

I am late to the party :) but here is how I generate a module, routing for the module and component all at one go and inside the same directory

From the src/app/ directory type in the following command to generate a module, routing and component called 'my-page'

ng g m my-page --routing=true && ng g c my-page --skip-tests=true -m=my-page

If you want the tests to be generated then do not use the --skip-tests argument.

Comments

15

Creates both module and routing simultaneously in the same folder.

ng g m sub-folder/module-name --routing

Creates the only module.

ng g m  sub-folder/module-name

enter image description here

Comments

13

To create a module with routing(lazy load importing), module routing, and a component that loaded at your routing you can write by following the syntax

ng g m [myModule_name] --route [myRoute_path_name] --module [routing_module_name]

Example :

ng g m public --route public --module app 

enter image description here

1 Comment

Another example ng g m features/courses --route courses --module app
6
  1. To generate component: ng g c componanentName or ng g c sub-folder/componentName
  2. To generate module or routing module use: ng g m sub-folder/moduleName --routing

Comments

5

Simple command for Create a Module with routing..

 ng g m [module_name] --routing

Comments

3

You can use

// module and routing
-> ng g m name --routing

// component with module and routing
-> ng g c name && ng g m name --routing
-> ng g m name --routing && ng g c name -m=name`

Comments

1
ng generate module ModulName --flat --module=app

Comments

1

You can generate a module, routing, and component all at one go like this:

ng g m vast-urls --routing=true && ng g c vast-urls --skip-tests=true -m=vast-urls

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
1

1-if you want generate a routing in a folder:

ng g m [modelName] --routing

2-if you want to generate a routing in the same folder:

ng g m [modelName] --flat --routing --modele=app

3-generate a componenet with model routing:

ng g c [modelName] && ng g m [modelName] --routing

Comments

1

PowerShell

This is a PowerShell function based on the other answers, which should be preloaded in the powersehll profile.

function Set-RoutingModule {
    
    param( [Parameter(Mandatory=$true)]
           [string]$Name );

    # Create Component
    & ng g c $Name  
     
    # Check the exit code
    if ($LastExitCode -eq 0) {
      # Create Module

      & ng g m $Name --routing=true

      & write-host "Success" -ForegroundColor Green
      
    } else {
      # Handle the error
      & write-host "Error executing ng g m command." -ForegroundColor Red
    }
}

Usage

 Set-RoutingModule About

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.