Home > Mobile >  Angular Implicit/Explicit Module Import
Angular Implicit/Explicit Module Import

Time:07-20

I currently have a SharedModule which every other module imports such that every module explicitly imports it, but because some modules import other modules, they then have an implicit import to the SharedModule as well. I am wondering if this is an issue for the bundle size or if Ivy just does some magic such that this is not an issue at all?

enter image description here

CodePudding user response:

From the Angular documentation:

What if I import the same module twice?

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'.

https://angular.io/guide/ngmodule-faq#what-if-i-import-the-same-module-twice

CodePudding user response:

Okay so no worries, Angular will create a single instance of your module, and no matter how many modules are importing your SharedModule, they will be dealing with only one instance.

Now pay attention for when you're using LazyLoading, their instance won't be created without loading the LazyLoaded module first.

  • Related