singleton - Pass global service to child classes in Angular 2 -
i've got main component has 2 child components , service. bootstrap(main, [myservice])
, want have functionality (possibility call service functions) these both child components. how should pass 1 instance of service parent component child components?
don't specify provider of service in component, include in constructor.
if you're providing service in bootstrap , want singleton, component this.
@component({ template: '<child-component></child-component>', directives: [childcomponent], providers: [] // no need include specify service here }) export class mycomponent { /*even though you're injecting service here, use same instance provided in bootstrap */ constructor(private myservice: myservice) { } }
@component({ selector: 'child-component', providers: [] // no need include specify service here }) export class childcomponent { //you can long don't specify service in providers list constructor(private myservice: myservice) { } }
Comments
Post a Comment