Suppose there is a parent component P . Having two child components B and C . I provide a service (MyService let's say) in the ElementInjector of P and Inject the service in the constructor of P component class. I Inject the service again in the two child components B and C .
My question is whether the instance of the service available with B,C and P will be the same in this case ?
And what if I Inject the service only in the two child components B and C and NOT in the parent component P . Will B and C have separate Instances of the MyService service in this case ?
Here is an image to help visualize the components .
Edit:
Service class :
@Injectable()
export class MyService(){
constructor(){
console.log('My Service instantiated');
}
}
P component's class:
import {MyService} from '...';
@Component({
selector:'Pcomp',
...
providers:[MyService]
})
export class PComponent {
constructor(public myservice:MyService){
}
}
Component B class :
import {MyService} from '...';
@Component({
selector:'Bcomp',
...
})
export class BComponent {
constructor(public myservice:MyService){
}
}
Component C class:
import {MyService} from '...';
@Component({
selector:'Ccomp',
...
})
export class CComponent {
constructor(public myservice:MyService){
}
}