provideIn any 和 root 有什么区别 [英] What is the difference between providedIn any and root

查看:44
本文介绍了provideIn any 和 root 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在我使用 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 https://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 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆