Angular 6/7 默认使用 Eager/Lazy 加载? [英] Angular 6/7 by default uses Eager/Lazy loading?
问题描述
正如标题所说,我们需要为模块手动实现延迟加载还是 Angular 6/7 默认这样做?
As the title says, do we need to manually implement Lazy Loading for the modules or does Angular 6/7 does it by default?
推荐答案
Angular 默认使用预加载,除非另有说明.
Angular by default uses eager loading unless specified otherwise.
要实现延迟加载,需要在RouterModule
中指定以下内容:
To implement Lazy Loading, following things need to be specified to the RouterModule
:
一个带有
loadChildren
而不是component
的路由配置对象.分配给它的值将是延迟加载模块的相对路径,后跟#
后跟模块的名称.
A route config object with
loadChildren
instead ofcomponent
. The value assigned to it would be the relative path to the Lazy Loaded Module, followed by#
followed by the name of the module.
延迟加载的模块也应该在其中实现一个路由模块,在那里,它应该调用 forChild
而不是 forRoot
.
The Lazy loaded module should also implement a routing module in it and in there, it should call forChild
instead of forRoot
.
Angular's Official 上提供了一个非常好的指南您可以遵循的文档来实现模块的延迟加载.
There's a very nice guide provided on Angular's Official Docs that you can follow to implement Lazy Loading of modules.
这是一个示例 StackBlitz 您可以遵循以实现延迟加载.
Here's a Sample StackBlitz that you can follow to implement Lazy Loading.
这篇关于Angular 6/7 默认使用 Eager/Lazy 加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!