简体   繁体   English

与在应用程序组件中使用服务作为提供者相比,providedIn: 'Root' 之间有什么区别?

[英]What is the difference between providedIn: 'Root' than using a service as a provider at the app component?

If I set a service as a provider at the app module (root module) or at the decorator @Injectable use providedIn: 'root', I assume that I will achieve the same behavior in both cases.如果我将服务设置为应用程序模块(根模块)或装饰器 @Injectable 使用提供的提供者:'root',我假设我将在两种情况下实现相同的行为。 That means all my application will "see" the same singleton class provided by the root module.这意味着我的所有应用程序都将“看到”根模块提供的同一个单例类。

But If I don't set any module at the @Injectable decorator on the service and use that class as a provider at the app component module, I am not able to see that same service within components that are not 'created' by my template.但是,如果我不在服务的 @Injectable 装饰器上设置任何模块,并在应用程序组件模块中使用该类作为提供者,我将无法在不是由我的模板“创建”的组件中看到相同的服务. For example, I can't use that service inside a MatDialog, that is created and opened using TS and not my template.例如,我不能在 MatDialog 中使用该服务,该服务是使用 TS 而不是我的模板创建和打开的。

That being said, what is the technical difference between those approaches?话虽如此,这些方法之间的技术区别是什么?

They introduced providedIn: 'root' to make it easier to manage services.他们引入了providedIn: 'root'来更容易地管理服务。 Most services are stateless singleton services.大多数服务是无状态的单例服务。 providedIn: 'root' is essentially the same as just adding the service at the app.module level in the providers array. providedIn: 'root'本质上与在 providers 数组中的 app.module 级别添加服务基本相同。 However, it was easy to forget to add this entry and an unnecessary extra step.但是,很容易忘记添加此条目和不必要的额外步骤。

Obviously, if you don't want a singleton service and would rather provide stateful services at various levels then the providers array is still available.显然,如果您不想要单例服务,而是希望在各个级别提供有状态的服务,那么提供者数组仍然可用。 It would be confusing to scrap it at the app.module level just because providedIn: root covers this.在 app.module 级别废弃它会令人困惑,因为providedIn: root涵盖了这一点。

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

相关问题 providedIn any 和 root 有什么区别 - What is the difference between providedIn any and root 使用与 providedIn 相同的服务:root 作为单例和组件的提供者与服务 - Using same service with providedIn: root as singleton and providers of component with the service 提供Angular 7服务:'root' - Angular 7 Service providedIn: 'root' 在“root”中提供的 angular 服务未定义 - providedin 'root' angular service is undefined 构造函数中的服务与作为组件中的提供者列出的服务之间的区别? - Difference Between Service in the Constructor Versus Listed as a Provider in a Component? Angular 提供的服务:'root' 为空 object - Angular service with providedIn: 'root' is an empty object Angular 2-7中的PROVIDER,INJECTOR和SERVICE有什么区别? - What is the difference between PROVIDER, INJECTOR and SERVICE in Angular 2-7? 注入@Component中的提供程序和@Module之间的Angular2有什么区别? - What is the difference in Angular2 between inject a provider in @Component and @Module? 在组件中使用服务变量更改与从服务中订阅事件之间有什么区别? - What is the difference between using the service variable change in a component versus subscribing to an event from the service? 演示应用程序中的提供根服务 - ProvidedIn Root Services within a Demo App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM