简体   繁体   English

使用与 providedIn 相同的服务:root 作为单例和组件的提供者与服务

[英]Using same service with providedIn: root as singleton and providers of component with the service

I would like to know what happens if the following criteria happen.我想知道如果满足以下条件会发生什么。

Created a service as single-ton with providedIn property as root.使用providedIn 属性作为根创建了一个作为单例的服务。 So that it will be available to the application globally.这样它就可以在全球范围内对应用程序可用。 At the same time, providers meta data in component or module set to the created service.同时,provider 组件或模块中的元数据设置为创建的服务。 Can any one tell me what is the effect on this situation?谁能告诉我对这种情况有什么影响?

To get more about Singletons you need to refer to official documentation .要获得有关 Singletons 的更多信息,您需要参考官方文档 Answering your question its quite simple.回答你的问题很简单。 Singletons are globals for entire application and share the same data.单例是整个应用程序的全局变量并共享相同的数据。 For example, you have a service which adds a script to the head or authentication service which holds global auth status - you need to ensure that they have only one instance across entire application.例如,您有一个服务将脚本添加到保持全局身份验证状态的头部或身份验证服务 - 您需要确保它们在整个应用程序中只有一个实例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM