简体   繁体   English

Angular:无法将商店注入服务

[英]Angular: Can't inject store into service

I am trying to inject a store into a service, but I am receiving the following error: 我试图将商店注入服务,但我收到以下错误:

Cannot read property 'select' of undefined 无法读取属性'select'的undefined

This is being raised on this line: 这是在这条线上提出的:

this.authUser$ = this.store.select(GetAuthUser);

For some reason, the framework is having difficulty creating the store. 出于某种原因,该框架难以创建商店。 The whole constructor of the service looks like this: 该服务的整个构造函数如下所示:

constructor(private store: Store<State>) {
  super();

  this.authUser$ = this.store.select(GetAuthUser);
}

The State looks like this: State看起来像这样:

export interface State {
  navigation: fromNavigation.State;
  router: RouterReducerState;
  auth: fromAuth.State;
}

I have tried being more specific with the State and using just the authState.State , but this doesn't work. 我尝试使用State更具体,只使用authState.State ,但这不起作用。

I have used the same code in a component, using the same state Store<State> in the constructor, and it works fine. 我在组件中使用了相同的代码,在构造函数中使用相同的状态Store<State> ,它工作正常。 It's just using it in a service is the problem. 它只是在服务中使用它是问题所在。

I'm sure it's something pretty simple, but I can't figure it out. 我确信这很简单,但我无法理解。 What could be wrong? 可能有什么不对? Do you need any more information? 您还需要更多信息吗?

Ok, so it turns out I was being an idiot. 好吧,事实证明我是个白痴。

The service that had the store in its constructor was not being created by DI, it was being created in a factory. 在其构造函数中具有存储的服务不是由DI创建的,而是在工厂中创建的。 Now I need to work out how to inject the store into a factory! 现在我需要弄清楚如何将商店注入工厂!

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

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