I've seen this thread: Using multiple instances of the same service. Is it possible to use same multiple instance of a service from parent to children?
For example, I have an ElementService
in ParentComponent
and have 2 instances of that service
{ provide: 'instance1', useClass: ElementService},
{ provide: 'instance2', useClass: ElementService},
How should I use instance1
in Child1Component
and instance2
in Child2Component
?
You can inject a named provider in the constructor of each child component (and in the parent constructor as well) :
Child1Component
Child2Component
Here is a working example with a counter : https://stackblitz.com/edit/angular-ivy-usorby?file=src%2Fapp%2Fchild1%2Fchild1.component.ts