Routing between modules in Angular 2

◇◆丶佛笑我妖孽 提交于 2019-12-02 16:42:31
Madhu Ranjan

It's not necessary to import components into main(app) module,

If you are loading routes lazily you may just define path like below,

// In app module route
{
 path: 'dashboard',
 loadChildren: 'app/dashboard.module#DashboardModule'
}

// in dashboard module
const dashboardRoutes: Routes = [
  { path: '',  component: DashboardComponent }
];

export const dashboardRouting = RouterModule.forChild(dashboardRoutes);

@NgModule({
  imports: [
   dashboardRouting
  ],
  declarations: [
    DashboardComponent
  ]
})
export class DashboardModule {
}

OR

You may just import the DashboardModule in the main module and it will work if the routes are not lazily loaded.

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    DashboardModule,
    routing
  ],
  declarations: [
    AppComponent
  ],
  providers: [
    appRoutingProviders
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

Hope this helps!!

It turns out that lazy loading didn't work properly in RC5, just upgraded to RC6 and it all worked.

Although that logic is nice, i would put all the rights in an accountmanagement component module for future tick on and off on features, while you don't want for your users to have all the rights you might want to upgrade a user account in the future. Expandability and foresee save's you time from future coding if your system is indeed keen on that logic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!