Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't I provide services in components in Angular 2

If I want to use service across the whole app, I should declare it as provider in app.module.ts, but it also works if it is declared in app.component.ts.

Is there any significant difference in those two approaches?

like image 898
darko99 Avatar asked Feb 05 '23 17:02

darko99


1 Answers

Let's say you have three components that are declared in your AppModule. If you provide service individually in every component, each of them will have its own instance of service, but if you provide it in AppModule, your service will be singleton - each component will share one same instance of that service.

like image 62
Stefan Svrkota Avatar answered Mar 06 '23 06:03

Stefan Svrkota