问题描述
正如标题所述,我们是否需要为模块手动实现延迟加载,还是默认情况下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官方提供了一个非常不错的 指南 您可以遵循的文档来实现模块的延迟加载.
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加载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!