https://angular.io/guide/ngmodule-faq#what-if-two-modules-provide-the-same-service
What if two modules provide the same service? When two imported modules loaded at the same time, list the provider with the same token, the second module provider wins. This is because both providers are added to the same injector.
When Angular tries to introduce a service for this token, it creates and provides an instance created by the second provider.
Each class that introduces this service receives an instance created by a second provider. Even classes declared in the first module receive an instance created by the second provider. This may be an undesirable surprise.
If module A provides a service for token "X" and imports module B which also provides a service for token "X", then for the service "Module A" the definition is "winning."
The service provided by the root AppModule takes precedence over the services provided by the imported modules. AppModule always wins.
If you provide the provider in the AppModule , this will be used for the providers provided in the imported modules.
This is different from lazy loadable modules. If the provider is provided in a lazy loaded module, the components and services lazy loaded by this module will become dependent on the lazy loaded module provider, even if it is provided in the AppModule.
Günter zöchbauer
source share