Angular 2 service start

Is it possible to start the angular 2 service at startup? bootstrap does not execute a service counter. And also the injector executes the service controller every time there is an injection of the service in some component. is there any way to make singelton service and make sure ctor will work only once?

+4
source share
2 answers

Add service only for bootstrapping, not for excerpts in your components. If you add it to your AppComponent, it will be created only once.

Adding a service to providers in a component creates a new instance for each instance of the component.

+2
source

Bootstrap , , . , .

(LogRepository) , . ( LogRepository) bootstrap(TheApp, [HTTP_PROVIDERS,LogRepository]);

, , , bootstrap(). .

+1

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


All Articles