1

I have different components and routes based on roles. Now i have defined routes as for certain components as below

Login Module 
path: '', loadChildren:'app/login/login.module#LoginModule';

Register Module 
path:'', loadChildren: 'app/register/register.module#LoginModule'

Dashboard Module
path:'' loadChildren:'app/dashboard/dashboard.module#dashboardModule'

User Profile Module
path:'' loadChildren:'app/user-profile/user-profile.module#UserProfileModule'

Now whenever I navigate to login or register page all the listed module chunks are getting loaded all together and page is slightly delayed and stucks until module chunks are not loading. Does anyone has idea how to define path and stop the unwanted module load on specific path. Thanks in advance for any help

6
  • why is the path same for all the routes ? Commented Feb 13, 2018 at 6:35
  • What path should i give ?? as i am accessing the module as below mydomain/login, mydomain/register, mydomain/dashboard, mydomain/user-profile Commented Feb 13, 2018 at 6:36
  • can you post your whole routing file ? Commented Feb 13, 2018 at 6:39
  • Yup wait a minute Commented Feb 13, 2018 at 6:39
  • const routes: Routes = [ { path: '', component: LoginComponent }, { path: '', component: RegisterComponent }, ]; export const routing = RouterModule.forChild(routes); Commented Feb 13, 2018 at 6:45

1 Answer 1

1

You will need to specify a route under your module will be executed.

Note that all lazy-loaded module must follow these three rules.

  1. The module which you want to lazy load they should have one parent route.
  2. Dont import module in your app.module.ts. Because if you do then no point of lazy loading
  3. Your will need to configure under one route in your app.routes.

So to answer your question you will need config like this..

{
// Login Module 
path: 'login', loadChildren:'app/login/login.module#LoginModule';

// Register Module ,
path:'register', loadChildren: 'app/register/register.module#LoginModule'

// Dashboard Module
path:'dashboard', loadChildren:'app/dashboard/dashboard.module#dashboardModule'

// User Profile Module
path:'profile', loadChildren:'app/user-profile/user-profile.module#UserProfileModule'

}

So whenever you will hit login route it will load the #LoginModule. In your login module should export routes like this

const routes: Routes = [
    { 
        path: '', 
        children: [
           {
           path: '',
           component: YourComponentToLoad 
           }
        ]
    }
]

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})

export class LoginRoutingModule {

}

Also, in your app-routing.module.ts you can define preloading strategy as following so that after the app is initialized, lazy modules will start loading immediately. So, when user clicks on a link and wants to see a lazy loaded module, it'll be ready.

app-routing.module.ts

@NgModule({
    imports: [RouterModule.forRoot(routes, {preloadingStrategy: PreloadAllModules})],
    exports: [RouterModule]
})
export class AppRoutingModule {
Sign up to request clarification or add additional context in comments.

20 Comments

Also, you should use preloadingStrategy in your root Routing module in which you call RouterModule.forRoot so that lazy loaded modules will start loading immediately after the app is initialized.
Ya I am using preloadingStartegy but what if the routes are protected by guards. I have certain components that are protected by routed and defined in one single module that how to define path for those components
@user5309516 are you using canActivate guard?
Yes I am using canActivate
Yes, then it would work fine. I use the same approach.
|

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.