Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between providedIn any and root

In Angular 9 the injectable decorator option providedIn has a new value called any. What is the difference between root and any?

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

@Injectable({providedIn: 'any'})
class UsefulService {
}
like image 616
Muhammed Albarmavi Avatar asked Jan 24 '20 07:01

Muhammed Albarmavi


People also ask

What is providedIn any?

ProvidedIn: any That means there might be multiple instances of the same service. That means that every lazy loaded module has it's own instance of the service. All eagerly loaded modules share one instance provided by the root module injector.

What does providedIn root do?

The providedIn allow us to specify how Angular should provide the dependency in the service class itself instead of in the Angular Module. It also helps to make the service tree shakable i.e. remove the service from the final bundle if the app does not use it.

What is the difference between providedIn and providers in Angular?

providedIn is the new Angular way of doing DI. providedIn was brought since Angular 6. 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.

What is providedIn in injectable?

providedInlinkDetermines which injectors will provide the injectable.


Video Answer


1 Answers

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

like image 134
akpgp Avatar answered Oct 04 '22 20:10

akpgp