N
N
No Name2017-01-23 16:40:55
JavaScript
No Name, 2017-01-23 16:40:55

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

2 answer(s)
E
emp1re, 2017-01-24
@alienworkshop

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',
    }

Create a new module users.module
imports: [  UserRoutingModule ]
declarations: [ UserComponent ]

In user.component. html and all static elements (SidebarModule and UserlistModule)
In user-routing we write all the routing we need for this module -
@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
  ]
})

A
Alexander Belov, 2017-01-23
@AlexanderBelov

If I understand correctly, we specify moduleId in the decorator

@Component({
      moduleId: module.id,
      selector: 'sidebar-module'
      ...
    });

In routes we write
export const routes: Routes = [
...
  { path: 'users/:id', component: SidebarModule }
 ...
];

https://angular.io/docs/ts/latest/cookbook/compone...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question