Angular service providedIn VS forRoot

Using providedIn vs providers[]:

  1. providedIn is the new Angular way of doing DI. providedIn was brought since Angular 6

  2. The official name is "Tree-shakeable providers" - instead of module providing all its services, it is now the service itself declaring where it should be provided

  3. Using providedIn: 'root' removes the need to import the library module at all, we can simply inject needed services and it just works


Yes, forRoot and provideIn both are equivalent since both will create the only and only one singleton for the app. Even though it being loaded in lazy loaded component.

Refer this nice article on it - https://medium.com/@chrishouse/when-to-use-angulars-forroot-method-400094a0ebb7

Tags:

Angular