Will Angular inject more than one instance of a generic Service if it is invoked across Component constructors using different Types?
I have a lot of Services that are going to provide the same functionality for different classes that all inherit from the same base class. I'd like to use Typescript's generic pattern MyService<T extends BaseClass>
to handle this.
But I don't know how it jives with Angular's injector:
- Will the Angular injector create more than one instance of this service if it's injected into different components with different Types?
- Will the injector inject the same instance if it is called with the same type?
Code:
@Injectable
export class MyService<T> {
...
}
@Component
export class ComponentA {
constructor(alphaService MyService<Alpha>) {} <--Instance 1
}
@Component
export class ComponentB {
constructor(betaService MyService<Beta>) {} <----Instance 2?
}
@Component
export class ComponentA1 {
constructor(alphaService MyService<Alpha>) {} <---Instance 1?
}
Is this legal? Does the injector know to create two instances of MyService?