I have a service MyService which needs to be defined in a module ModuleCommon, and used in modules ModuleA and ModuleB. The dependencies of MyService vary between ModuleA and ModuleB.
Example:
export class MyService {
constructor(@Inject(XInjectionToken) dep: IDependency) { }
}
In ModuleA, IDependency implementation should be a ModuleADependency, whereas in ModuleB it's a ModuleBDependency.
How can I add MyService definition to ModuleCommon and 'defer' the injection of XInjectionToken until it can be provided by the child module?
The solution was to add
MyServiceto the providers array ofModuleCommondirectly, without using the{ provide: InjectionToken, useClass: MyService }syntax. That appears to register the service with DI without instantiating it.