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?