The difference between the provision of the NgModule vs Component service

We are only in our first project with angular2. I have a question about providing a service.

As I know, there are two ways to declare a provider with providers:[MyService]in your application. You can declare it globally in a tag @NgModuleor locally in a tag @Component.

As far as I know, the only difference between the two methods is the scope of the provision. When an application is widely distributed, only one component. From this, I would conclude that I should prefer to provide the service mainly (depending on the use of the service) locally in my particular component, in order to reduce the scope.

Is this correct, or are there any other differences between the two methods of declaration that I don’t know about?

+4
source share
1 answer

If you provide a local service to your components, they will NOT have SUCH service. They receive all services of the same type, but they will not be the same.

If you provide them in the AppModule, it will be created as a singleton for the entire application.

+4
source

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


All Articles