简体   繁体   English

可以在Angular 8中预加载延迟加载的模块吗?

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

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. 所以假设用户看到主屏幕,如果在3秒内没有完成任务,那么在后台加载我的一些关键应用模块。

You can use the preload strategy just for one module by setting the data: { preload: true } instead of all the modules using PreloadAllModules . 通过使用PreloadAllModules设置data: { preload: true }而不是所有模块,您可以仅为一个模块使用preload策略。

{
  path: 'crisis-center',
  loadChildren: () => import('./crisis-center/crisis-center.module').then(mod => mod.CrisisCenterModule),
  data: { preload: true }
},

Yes you can use preloadingStrategy 是的,你可以使用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 这是一篇很好的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM