How to add a routing module to an existing module in angular CLI version 1.1.1?

Viewed 76797

I have created a module in using angular CLI and forget to add --routing while it's creation, now i want to add a routing module to it.

ng init --routing won't help in angular CLI version 1.1.1

2 Answers
  1. ng generate module app-routing --module app --flat
    • Replace app with the name of your existing module
    • Replace app-routing with the name to give the routing module
  2. In the new module, add appRoutes and RouterModule.forRoot or RouterModule.forChild.

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { Routes, RouterModule } from '@angular/router';
    
    const appRoutes: Routes = [
        { path: 'some-path', component: SomeComponent }
    ];
    
    @NgModule({
        imports: [
            RouterModule.forRoot(appRoutes)
        ],
        exports: [
            RouterModule
        ]
    })
    export class AppRoutingModule {}
    
  3. Add <router-outlet></router-outlet> to your component if needed (e.g., app.component.html)

Source

Related