Answer the question
In order to leave comments, you need to log in
Angular 2: How to make a separate module load on a separate route, and not the entire AppModule?
It is necessary to divide the entire application into modules, while each feature / component part (for example, Sidebar) must have its own module. How can I make sure that only the necessary modules are loaded on a separate route?
For example, on the /users page, you need to load the SidebarModule and UserlistModule.
Answer the question
In order to leave comments, you need to log in
We import a new module
in app.module In the app routing, you need to specify the parent routing for UsersModule
{
path: 'users',
loadChildren: './users/users.module#UsersModule',
}
imports: [ UserRoutingModule ]
declarations: [ UserComponent ]
@NgModule({
imports: [
RouterModule.forChild([
{
path: '',
component: UserComponent,
canActivate:[AuthGuardService],
children: [
{
path: '',
children: [
{
path: 'users',
component: ElseComponent,
canActivate:[AuthGuardService]
},
{
path: '',
redirectTo: 'users',
canActivate:[AuthGuardService]
}
]
}
]
}
])
],
exports: [
RouterModule
]
})
If I understand correctly, we specify moduleId in the decorator
@Component({
moduleId: module.id,
selector: 'sidebar-module'
...
});
export const routes: Routes = [
...
{ path: 'users/:id', component: SidebarModule }
...
];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question