简体   繁体   English

无法将服务注入另一个服务(单例),Angular2

[英]Trouble injecting service into another service (singleton), Angular2

I have two services, both are @Injectable. 我有两个服务,都是@Injectable。 ServiceA is provided to components via the Providers array and works great everywhere. ServiceA通过Providers阵列提供给组件,并且可以在任何地方使用。 ServiceB is a singleton and is in the Providers array of its module and works great everywhere. ServiceB是一个单例,位于其模块的Providers数组中,无处不在。 ServiceA needs to remain not a singleton, and ServiceB needs to remain a singleton. ServiceA需要保持不是单身,而ServiceB需要保持单身。

My issue is that now I need to access ServiceA from ServiceB, but don't know how to access it. 我的问题是,现在我需要从ServiceB访问ServiceA,但不知道如何访问它。 Seems I cannot inject a non-singleton service into a singleton. 似乎我无法将非单身人士服务注入单身人士。

Everything I come across tells me to put the service in bootstrap or module declaration but as I mentioned I cannot do that as I need ServiceA to behave as a non-singleton. 我遇到的一切都告诉我将服务放在bootstrap或模块声明中,但正如我所提到的,我不能这样做,因为我需要ServiceA表现为非单例。

// module setup
... providers: [ServiceB]

// non-singleton
@Injectable()
export class ServiceA

// singleton
@Injectable()
export class ServiceB {
  constructor(ServiceA)  // <-- this is the problem area, need to get ServiceA here

I have tried doing something like constructor(Inject(ServiceA) serviceA) but seem to get the same errors with that 我尝试过像构造函数(Inject(ServiceA)serviceA)这样的东西,但似乎得到了相同的错误

Error: DI Error No provider for ServiceA! 错误:DI错误没有ServiceA的提供商!

  1. How can I "provide" a non-singleton service into a singleton service, where both are @Injectable? 如何将非单件服务“提供”到单件服务中,两者都是@Injectable?

You can declare ServiceA both in @Component.providers and @NgModule.providers : 您可以在@Component.providers@NgModule.providers声明ServiceA

  • When injecting ServiceA in a component's constructor, you'll get the component-scoped instance (corresponding to the @Component.providers declaration). 在组件的构造函数中注入ServiceA时,您将获得组件范围的实例(对应于@Component.providers声明)。
  • When injecting ServiceA in another service's constructor (in your case: when injecting ServiceA in ServiceB ), you'll get the global instance (corresponding to the @NgModule.providers declaration). 当注入ServiceA在另一个服务的构造函数(你的情况:当注射ServiceAServiceB ),你会得到全局实例(对应于@NgModule.providers声明)。

In other words, if you redeclare a service in a component's providers ( @Component.providers ), it will "shadow" the instance that was declared globally in @NgModule.providers . 换句话说,如果您在组件的提供程序( @Component.providers )中重新声明服务,它将“ @NgModule.providers@NgModule.providers全局声明的实例。

WORDS OF CAUTION: 小心的话:

  • If you inject ServiceA into ServiceB AND ServiceC , then B and C would get the same instance of A. 如果将ServiceA注入ServiceBServiceC ,那么B和C将获得相同的A实例。
  • As mentioned by Seaal in the comments, organizing the code in this manner might not be aligned with best practices. 正如Seaal在评论中所提到的,以这种方式组织代码可能与最佳实践不一致。

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

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