0

I am trying to figure out how to empoy @NgModule to better organize an Angular 2 modular app. In particular, I am interested in an NgModule installing its own routes into the app. Does anyone have a working example showing how to go about it?

user776686
  • 7,933
  • 14
  • 71
  • 124

1 Answers1

0

Let's say for example you have the home module.

-home
 -- home.component.ts
 -- home.component.html
 -- home.component.spec.ts
 -- home.routes.ts

// home.routes.ts

import { Routes, RouterModule } from "@angular/router";
import { HomeComponent } from "./home.component";
const routes : Routes = [
  {
    path: '',
    component: HomeComponent
  }
]

export default RouterModule.forChild(routes)

Then in your AppModule top level routes:

const routes : Routes = [
  {
    path: '',
    loadChildren: 'app/modules/home/home.module',
    pathMatch: 'full'
  }
]

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  export class AppModule {
    constructor() {
   }
  }

With this your home module will be lazy loading.

Bazinga
  • 10,716
  • 6
  • 38
  • 63
  • Thanks. I fell into some kind of infinite loop somewhere, resulting in "stack exceeded" error, but that must have been my bad. Accepting the answer. – user776686 Oct 24 '16 at 08:36