Generate a routing module while creating a module in angular-cli

167,215

Solution 1

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

So basically, there's 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.

Solution 2

Module with Routing Create CMD :-

ng g m [ModuleName] --routing

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
167,215
Saiyaff Farouk
Author by

Saiyaff Farouk

Engineering Software B Eng(Hons). Software Engineering at Informatics Institute of technology Catch me on - LinkedIn, Twitter, Github, Facebook, Medium

Updated on February 08, 2022

Comments

  • Saiyaff Farouk
    Saiyaff Farouk about 2 years

    I recently started implementing lazy loading in my application. I was wondering whether there is any way to create a routing.module.ts while generating a new module in angular-cli application other than creating it manually?

  • Dhana
    Dhana over 3 years
    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 .
  • Saiyaff Farouk
    Saiyaff Farouk over 3 years
    @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
  • w0l-g0r
    w0l-g0r about 2 years
    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.