[英]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
? root
和any
什么区别?
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 : root
和any
根据官方文档的区别:
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
和模块选项之外,现在我们还有两个额外的选项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.
🔵 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.