Angular: what is the difference between declaring a service in a component and the boot process

What is the difference between this:

bootstrap(AppComponent, [HeroService]);

And this:

@Component({
  selector: 'test',
  providers: [HeroService]
})
+4
source share
3 answers

If you add it only to bootstrap(), you will get a singleton, if you add it to a component, you will get a new instance for each instance of the component. If you add it to both, it depends on where you enter it. Angular2 has a hierarchical DI. He is looking up for the provider, the first one returned is returned.

See also http://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html

+2
source

HeroService bootstrap, HeroService - , .

providers .

Angular 2.

+1

, . , .

, .

+1

Source: https://habr.com/ru/post/1624672/


All Articles