问题
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 {
}
回答1:
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
回答2:
angular 9 introduce new option for injectable decorator ProvidedIn in addition to the previous root
and module options, now we have two additional options
platform
, any
🔵 root— This tells Angular to provide the service in the application root level and the service will be created once (singleton service ) and provide the same instance in every module that injects the token.
🔵 any— Provides a unique instance in every module (including lazy modules) that injects the token.
🔵 platform— Specifying providedIn: 'platform' makes the service available in a special singleton platform injector that is shared by all applications on the page.
a detailed look at Angular's 'root' and 'any' provider scopes
来源:https://stackoverflow.com/questions/59892369/what-is-the-difference-between-providedin-any-and-root