ncohen ncohen - 1 year ago 222
TypeScript Question

Share one service instance in Angular 2 RC5

I used to share one service instance by declaring it as a

inside my
like this:

selector: 'my-sel',
viewInjectors: [SharedService],
templateUrl: 'template.html',
pipes: [MyPipe]

This solution doesn't work anymore in Angular 2 RC5. Any thoughts?

Answer Source

If you want to share a service for your application or your module, you need to declare it when bootstrapping the application or when defining your module.

bootstrap(AppComponent, [ SharedService ]);


  declarations: [
  providers: [SharedService], // <----
  bootstrap: [ AppComponent ]
export class AppModule { }

Don't forget to remove the service from providers attributes of your components...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download