6

I am struggling to see which one of the root and the platform options for providedIn in a service is better.

Both make a service available in the whole application, the latter through a service and the former through the root injector. Any advice on this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Scipion
  • 11,449
  • 19
  • 74
  • 139
  • Upvoted because I'm also interested in knowing the answer; I would assume it has to do with the improved lazyloading capabalities that come with angular 9. – Maartenw Feb 11 '20 at 08:32
  • 1
    Seems like we already have discussed this. https://stackoverflow.com/questions/59892369/what-is-the-difference-between-providedin-any-and-root – sunny singh Feb 11 '20 at 08:33
  • Does it mean that if I run a single app, both are equivalent ? – Scipion Feb 11 '20 at 08:35
  • 2
    Have you read https://angular.io/api/core/Injectable? Unless you have multiple applications in a page, it doesn't matter. – jonrsharpe Feb 11 '20 at 08:35
  • I certainly did, and while "yes it doesn't matter" maybe it would matter performance wise. – Scipion Feb 11 '20 at 09:23

1 Answers1

2

The difference between the root and any as per offical documentation :

  • root : The application-level injector in most apps.

  • platform : A special singleton platform injector shared by all applications on the page.

  • any : The NgModule injector that receives the resolution.

For more details please refer this article.

Is a service considered a singleton in the case that I use any? - No

To go back to the original question between root & platform, when using a single application in your project, it doesn't change anything performance wise.

Scipion
  • 11,449
  • 19
  • 74
  • 139