This is my SharedModule
import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { IconModule } from "@my-app/components/icon/icon.module";
import { RequiredActionDirective } from "@my-app/directives/required-action.directive";
@NgModule({
imports: [
CommonModule,
IconModule
],
declarations: [
RequiredActionDirective
],
exports: [
CommonModule,
IconModule,
FormsModule,
ReactiveFormsModule,
RequiredActionDirective
]
})
export class SharedModule { }
Please note that I added to Imports only CommonModule and IconModule. I did it because I'm using those modules in my RequiredActionDirective. But Exports has more Modules because will be used by other modules that will import SharedModule.
Question: am I thinking correctly that I don't need to add Modules to Imports unless I want to use them directly in SharedModule? Or in future there might be some problems of which I am currently not aware because now everything is working properly?
You shouldn't re-export modules without a reason, and only import modules that the current module depends upon. There is no harm to import, but exporting creates a branch in the dependency tree for providers.
Once a module re-exports a module the parent module importing it can not override that import. So this can be a problem in some edge cases.
For example;
Now the AppModule can not provide another interceptor.
The reason is that the
HttpClientModulebecomes declared in the provider tree at theSharedModulelevel and resolves allHTTP_INTERCEPTORSfrom that point down.The requirement is that the
AppModuleshould import theHttpClientModulefirst, and then theSharedModule.Now the
HttpClientModuleis declared at theAppModulelevel and resolves all providers downwards including theSharedModule. The order in theimports: []array isn't important.