ncohen ncohen - 2 months ago 56
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?


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...