Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to pre load a lazy loaded module in Angular 8?

Tags:

angular

I know that when a app has a lot of parts and components it is good to separate them into lazy loaded modules so the user does see the app home page fast.

The thing is I have notice that the navigation to an lazy loaded module's component shows some lag between the user interaction (click in the button/menu) and when the component is show (compared with a non lazy loaded component).

Is there a way to pre load a lazy loaded module manually? so lets say the user sees the home screen and if nothing is done in 3 seconds then load some of my critical app modules in the background.

like image 569
distante Avatar asked Jan 25 '23 20:01

distante


2 Answers

You can use the preload strategy just for one module by setting the data: { preload: true } instead of all the modules using PreloadAllModules.

{
  path: 'crisis-center',
  loadChildren: () => import('./crisis-center/crisis-center.module').then(mod => mod.CrisisCenterModule),
  data: { preload: true }
},
like image 120
Sachila Ranawaka Avatar answered Jan 28 '23 09:01

Sachila Ranawaka


Yes you can use preloadingStrategy

You have to add it like this:

RouterModule.forRoot(appRoutes, {preloadingStrategy : PreloadAllModules } )

Example:

import { RouterModule, Routes, PreloadAllModules } from '@angular/router';

const appRoutes: Routes = [
  { path: 'employees', loadChildren: './employee/employee.module#EmployeeModule' },
  { path: 'home', component: HomeComponent },
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }
];

@NgModule({
  imports: [ RouterModule.forRoot(appRoutes, {preloadingStrategy : PreloadAllModules } )],
  exports: [ RouterModule ]
})
export class AppRoutingModule { } 

This is a good Article

like image 35
Adrita Sharma Avatar answered Jan 28 '23 09:01

Adrita Sharma