1

I am trying to do lazy loading angular2 module using router as shown in hero tutorial. In my network tab i can see the the files are getting downloaded in my browser but the component is not showing in my browser

//module
@NgModule({
  declarations: [
    CustomerDashboardComponent
  ],
  exports: [
    CustomerDashboardComponent
  ]
})
export class CustomerDashboardModule {
}

//component code
@Component({
  selector: 'customer-dashboard',
  templateUrl: 'customer-dashboard/customer-dashboard.html',
})
export class CustomerDashboardComponent extends OnInit{

  constructor(private router: Router,
              private homeService: HomeService,
              private restService: RestService
  ) {
  }
}

1 Answer 1

2

I don't see your router code to load your component from the lazy loaded module. once your module is loaded using loadChildren your module should have a default routing option to load the component. add default routing in your lazy loaded module like below

//router code
loadChildren: 'src/customer-dashboard/customer-dashboard.module#CustomerDashboardModule'

//default routing definition code
const routes: Routes = [
  { path: '', component: CustomerDashboardComponent }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);

//routing import in module
@NgModule({
  imports: [
    routing
  ],
  declarations: [
    CustomerDashboardComponent
  ],
  exports: [
    CustomerDashboardComponent
  ]
})
export class CustomerDashboardModule {
}

//component code
@Component({
  selector: 'customer-dashboard',
  templateUrl: 'customer-dashboard/customer-dashboard.html',
})
export class CustomerDashboardComponent extends OnInit{

  constructor(private router: Router,
              private homeService: HomeService,
              private restService: RestService
  ) {
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am able to see the module and component is loaded in the browser.
Yes. loadChildren is lazy loading the module and component but its not showing in the browser because of the router is missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.