在延迟加载模块中加载小 Angular 模块
Loading small Angular Modules in Lazy loaded modules
我在 Angular (CustomCoreModule) 中创建了一个模块,该模块在一段时间内增长,导致性能问题并使应用程序非模块化且不可维护。
我决定创建小模块并在需要的地方导入它们,而不是加载一个大模块(这会减小包的大小)。
如果我创建了一个模块 'A',它最终在两个延迟加载模块中使用,Angular 是否会针对两个不同的延迟加载模块优化模块两次,当它们被加载时或者,将Angular 尝试为每个模块再次加载模块 'A'。
我们正在使用 Angular 8,带有打字稿。另外,Angular 9 会在这个问题上带来任何好处吗?
如果导入同一个模块两次怎么办?
That's not a problem. When
three modules all import Module 'A', Angular evaluates Module 'A'
once, the first time it encounters it, and doesn't do so again.
That's true at whatever level A appears in a hierarchy of imported
NgModules. When Module 'B' imports Module 'A', Module 'C' imports 'B',
and Module 'D' imports [C, B, A], then 'D' triggers the evaluation of
'C', which triggers the evaluation of 'B', which evaluates 'A'. When
Angular gets to the 'B' and 'A' in 'D', they're already cached and
ready to go.
Angular doesn't like NgModules with circular references, so don't let
Module 'A' import Module 'B', which imports Module 'A'.
我在 Angular (CustomCoreModule) 中创建了一个模块,该模块在一段时间内增长,导致性能问题并使应用程序非模块化且不可维护。
我决定创建小模块并在需要的地方导入它们,而不是加载一个大模块(这会减小包的大小)。
如果我创建了一个模块 'A',它最终在两个延迟加载模块中使用,Angular 是否会针对两个不同的延迟加载模块优化模块两次,当它们被加载时或者,将Angular 尝试为每个模块再次加载模块 'A'。
我们正在使用 Angular 8,带有打字稿。另外,Angular 9 会在这个问题上带来任何好处吗?
如果导入同一个模块两次怎么办?
That's not a problem. When three modules all import Module 'A', Angular evaluates Module 'A' once, the first time it encounters it, and doesn't do so again.
That's true at whatever level A appears in a hierarchy of imported NgModules. When Module 'B' imports Module 'A', Module 'C' imports 'B', and Module 'D' imports [C, B, A], then 'D' triggers the evaluation of 'C', which triggers the evaluation of 'B', which evaluates 'A'. When Angular gets to the 'B' and 'A' in 'D', they're already cached and ready to go.
Angular doesn't like NgModules with circular references, so don't let Module 'A' import Module 'B', which imports Module 'A'.