provideIn any 和 root 有什么区别

编程入门 行业动态 更新时间:2024-10-26 13:24:20
本文介绍了provideIn any 和 root 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在 Angular 9 中,可注入装饰器选项 providedIn 有一个名为 any 的新值.root 和 any 有什么区别?

在我使用 any 的情况下,服务是否被视为单例?

@Injectable({providedIn: 'any'})类有用的服务{}

解决方案

我认为提供的答案不是很清楚.但是,@jkonst、@schrödingcöder 和 @Bruce 在评论中是正确的.

对于 Google 发送到这里的任何人,

any 不会在每个模块中提供唯一的实例.(应该只在每个共享模块中说)

这意味着每个注入范围

中有一个实例

来自

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 { }

解决方案

I think provided answers are not really clear. However, @jkonst, @schrödingcöder and @Bruce are correct in the comment.

For any one sent here by Google,

any doesn't Provides a unique instance in every module. (should say only in every shared module)

Which means one instance in every inject scope

from angular.io/guide/providers

With providedIn: 'any', all eagerly loaded modules share a singleton instance; however, lazy loaded modules each get their own unique instance, as shown in the following diagram.

更多推荐

provideIn any 和 root 有什么区别

本文发布于:2023-11-12 07:12:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   provideIn   root

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!