0

Suppose I have three modules:

  • App module
  • Core module
  • SomeFeature module

Then I

  • import both Core and SomeFeature modules in App Module
  • import Core module in SomeFeature module
  • add console.log('hello') to constructor of Core module

Why 'hello' is only printed once? therefore why Core module constructor is called once?

here's stackblitz: https://stackblitz.com/edit/angular-ivy-bdter8


I'm asking because I saw this piece of code:

export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule?: CoreModule) {
    if (parentModule) {
      throw new Error(
        'CoreModule is already loaded. Import it in the AppModule only'
      );
    }
  }
}

It is supposed to prevent Core module to be imported in modules other than App module. I wanted to give it a shot but I guess it works only in case of reimporting it in lazy loaded modules??

Also, how does injecting module even work? Where modules are provided? I recently understood how DI resolution works for services, but how does it work for modules?

1 Answers1

0

Not too sure about what you are trying to achieve, but on a healthy angular app, Module constructor's should only be called once.

You might end up with multiple instances of services if the module being loaded a second time provides services. Angular docs state that:

A lazy-loaded module has its own injector

Lazy-loaded services are scoped to the lazy-loaded module's injector

So, lazzy loading can definitely cause it... when the module is both used in the main app and in the Lazzy loaded one.

When you are not lazzy loading (which is the case of your stackblitz), and you have modules imported in different parts of your app, the module will only be constructed once. Its instance will be handled by the root ModuleInjector.

And that is the reason for your "hello" to print only once.

The Fabio
  • 5,369
  • 1
  • 25
  • 55
  • Thanks for your response! What I failed to understand is that the module instance itself is handled by Injector (and module itself can be injected). Where exactly can I find that statement in the [docs](https://angular.io/guide/hierarchical-dependency-injection)? There must be other way of learning that besides asking on stackoverflow. – starfruit3254 Mar 15 '22 at 08:50
  • Do you know when I can find that in the docs? I am about to close this question, but need to know this one thing :P – starfruit3254 Mar 16 '22 at 14:46