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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -