Why I should not provide services in components in Angular 2

If I want to use the service throughout the application, I have to declare it as a provider in app.module.ts, but it also works if it is declared in app.component.ts.

Is there a significant difference in these two approaches?

+4
source share
2 answers

Say you have three components declared in yours AppModule. If you provide the service individually in each component, each of them will have its own instance of the service, but if you provide it in AppModule, your service will be singleton - each component will share the same instance of this service.

+7

app.module.ts vs app.component.ts - - Angular. , . service-name.service.ts, app.module.ts. , ,

0

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


All Articles