Within a sub-module, there is a service that wraps a third-party module, instantiates it, and initializes its service for usage within the app.
@Injectable()
class SubmoduleInitializerService {
constructor (thirdPartyService: ThirdPartyService) {
thirdPartyService.initialize(...);
...
}
}
@NgModule({
imports: [ThirdPartyModule],
exports: [ThirdPartyModule],
providers: [
ThirdPartyService,
SubmoduleInitializerService
]
})
class AppSubmodule {}
The ThirdPartyService
is not directly injected in the app but is utilized by other units of the ThirdPartyModule
. As long as the SubmoduleInitializerService
is injected in the same injector as the ThirdPartyService
or in the parent injector, everything functions correctly:
export class AppComponent {
constructor(
/* DO NOT REMOVE! BAD THINGS HAPPEN! */
submoduleInitializerService: SubmoduleInitializerService
) {}
...
}
This setup has proven to be ineffective as it lacks clarity on why the SubmoduleInitializerService
needs to remain injected in the AppComponent
without being used in either the class or the template. This caused accidental removals in the past.
In essence, the AppSubmodule
module requires an alternative to Angular 1.x's angular.module(...).run(...)
block.
What are the possible solutions for this scenario?