简体   繁体   English

providedIn any 和 root 有什么区别

[英]What is the difference between providedIn any and root

In Angular 9 the injectable decorator option providedIn has a new value called any .在 Angular 9 中,可注入装饰器选项providedIn有一个名为any的新值。 What is the difference between root and any ? rootany什么区别?

Is a service considered a singleton in the case that I use any ?在我使用any的情况下,服务是否被视为 singleton ?

@Injectable({providedIn: 'any'})
class UsefulService {
}

The difference between the root and any as per offical documentation : rootany根据官方文档的区别:

  • root : The application-level injector in most apps. root :大多数应用程序中的应用程序级注入器。

  • platform : A special singleton platform injector shared by all applications on the page. platform :页面上所有应用程序共享的特殊单例平台注入器。

  • any : The NgModule injector that receives the resolution. any :接收解析的 NgModule 注入器。

For more details please refer this article .有关更多详细信息,请参阅这篇文章

Is a service considered a singleton in the case that I use any ?在我使用any的情况下,服务是否被视为单例? - No -没有

I think provided answers are not really clear.我认为提供的答案不是很清楚。 However, @jkonst, @schrödingcöder and @Bruce are correct in the comment.但是,@jkonst、@schrödingcöder 和@Bruce 在评论中是正确的。

For any one sent here by Google,对于 Google 发送到这里的任何一个,

any doesn't Provides a unique instance in every module. any不在每个模块中提供唯一实例。 (should say only in every shared module) (应该只在每个共享模块中说)

Which means one instance in every inject scope这意味着每次注入一个实例 scope

from https://angular.io/guide/providers来自https://angular.io/guide/providers

With providedIn: 'any', all eagerly loaded modules share a singleton instance;使用 providedIn: 'any',所有急切加载的模块共享一个 singleton 实例; however, lazy loaded modules each get their own unique instance, as shown in the following diagram.然而,延迟加载的模块每个都有自己独特的实例,如下图所示。

图表

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 angular 9 为可注入装饰器提供了新选项,除了之前的root和模块选项之外,现在我们还有两个额外的选项platformany

🔵 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. 🔵 root — 这告诉 Angular 在应用程序根级别提供服务,该服务将被创建一次单例服务)并在每个注入令牌的模块中提供相同的实例

🔵 any — Provides a unique instance in every module (including lazy modules) that injects the token. 🔵 any — 在每个注入令牌的模块(包括惰性模块)中提供一个唯一的实例

🔵 platform — Specifying providedIn: 'platform' makes the service available in a special singleton platform injector that is shared by all applications on the page. 🔵 platform — 指定 providedIn: 'platform' 使服务在一个特殊的单例平台注入器中可用,该注入器由页面上的所有应用程序共享。

a detailed look at Angular's 'root' and 'any' provider scopes 详细了解 Angular 的“root”和“any”提供者范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与在应用程序组件中使用服务作为提供者相比,providedIn: &#39;Root&#39; 之间有什么区别? - What is the difference between providedIn: 'Root' than using a service as a provider at the app component? WebStorm - 参数类型 {providedIn: "root"} 不可分配给参数类型 {providedIn: Type<any> | “根” | null} & InjectableProvider</any> - WebStorm - Argument type {providedIn: "root"} is not assignable to parameter type {providedIn: Type<any> | "root" | null} & InjectableProvider 是否使用@Injectable providedIn为任何非延迟加载的模块与提供的相同:“root”? - Is using @Injectable providedIn for any non-lazy-loaded module the same as providedIn: “root”? 任何和任何[]有什么区别? - What is the difference between any and any[ ]? 提供Angular 7服务:&#39;root&#39; - Angular 7 Service providedIn: 'root' @Injectable(providedIn: &#39;root&#39;) 的语义? - The semantics of @Injectable(providedIn: 'root')? 提供平台与根 - providedIn platform vs root Angular 7/8:Treeshakable 提供程序(提供在:'root') - Angular 7/8: Treeshakable providers (providedIn: 'root') 在“root”中提供的 angular 服务未定义 - providedin 'root' angular service is undefined Angular 提供了根和根模式 - Angular providedIn root and forRoot pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM