Nate
Nate

Reputation: 7856

Share one service instance in Angular 2 RC5

I used to share one service instance by declaring it as a viewInjectors inside my @Component like this:

@Component({
    selector: 'my-sel',
    viewInjectors: [SharedService],
    templateUrl: 'template.html',
    pipes: [MyPipe]
})

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

Upvotes: 1

Views: 374

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202146

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 ]);

or

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

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

Upvotes: 6

Related Questions