角度单例服务 [英] Angular singleton service

查看:58
本文介绍了角度单例服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,使用 providedIn:'root'和将您的提供程序添加到模块的 providers 数组之间的唯一区别是摇树.目前,如果我添加 providedIn:'root',我的应用程序可以正常工作,但是如果我将其删除并将其添加到模块的providers数组中,则会得到 StaticInjectorError 说找不到供应商.有没有人看到或了解为什么会发生这种情况?从文档中,我认为将其添加到providers数组应该可以使其工作

From my understanding the only difference between using providedIn: 'root' and adding your provider to the providers array of the module is for tree shaking. Currently my application works fine if I add providedIn: 'root' it works fine, but if I remove that and add it to the providers array of the module I get the StaticInjectorError saying it cant find the provider. Has anyone seen this or have an understanding of why this would happen? From the docs I believe that adding it to the providers array should allow this to work

推荐答案

该错误是不言自明的.如果您执行 providedIn:'root',则您的服务将在RootInjector上注册.因此,基本上可以在整个App中使用它,而不必手动将其添加到要在其中使用此服务的每个模块的 providers 数组.

The error is pretty self-explanatory. IF you do providedIn: 'root', then your service gets registered on the RootInjector. And hence it's basically available to use throughout the App without having to manually add it to the providers array of each of the module that you want to use this service in.

但是现在您已经从 @Injectable 装饰器中删除了 providedIn:'root',它仅适用于将其添加到这些模块的 providers 数组.否则将对导入将服务添加到 providers 数组的模块的模块可用.

But now that you have removed the providedIn: 'root' from the @Injectable decorator, it will only be available to the Modules where you are adding it to the providers array of those modules. Or it would be available to the modules who are importing the modules that have the service added to the providers array.

这是 示例StackBlitz 可帮助您更好地理解这一点.

Here's a Sample StackBlitz to help you understand this in a better way.

仅描述一下:

  1. 我那里有3个模块: AppModule NewModule NotImportedModule .
  2. 我将使用在 AppModule AppComponent NewModule NotImportedModule 中编写的服务>.
  3. NewModule SampleService 添加到 NewModule NewModule的 provides 数组中添加到 AppModule imports 数组中.这就是为什么我能够在 AppComponent 中使用 SampleService 的原因.
  4. NotImportedModule 未添加到 AppModule imports 数组中,但 AnotherService providedIn:"root" .因此,我可以在 AppModule 中使用它.
  1. I have 3 Modules in there: AppModule, NewModule, and NotImportedModule.
  2. I'll be using the services written in the NewModule and the NotImportedModule in the AppModule's AppComponent.
  3. NewModule's SampleService is added to the provides array of the NewModule and the NewModule is added to the imports array of the AppModule. And that's why I'm able to use the SampleService in the AppComponent.
  4. NotImportedModule is not added to the imports array of the AppModule but the AnotherService is providedIn: 'root'. Hence I'm able to use it in the AppModule.

这意味着 AnotherService 将可在整个App中使用,因为它已在RootInjector上注册,因为我们在其上使用了 providedIn:'root'.

Which would mean that AnotherService would be available to be used throughout the App since it is registered on the RootInjector, since we used providedIn: 'root' on it.

但是由于 SampleService 不是 provideredIn:'root',而是被添加到 NewModule providers 数组中>要在 AppModule 中使用它,我们必须将 NewModule 添加到 AppModule imports 数组中.

But since SampleService was NOT providedIn: 'root' but was added to the providers array of the NewModule to use it in the AppModule, we had to add the NewModule to the imports array of the AppModule.

希望这个例子更有意义.

Hope it makes more sense with this example.

这篇关于角度单例服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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