【问题标题】:[Angular][Store] Problem with injecting a service into another service[Angular][Store] 将服务注入另一个服务的问题
【发布时间】:2020-03-14 15:38:13
【问题描述】:

我遇到了以下问题。我创建了一个服务——我们称之为 CopyService。我正在将 Store 注入其中。

@Injectable({
  providedIn: 'root'
})
export class CopyService {  
  constructor(
    private copyStore: Store<fromCopyStore.CopyState>,
  ) {
    //something going on with the store
  } 

}

接下来,我将 CopyService 注入到 MenuService 中

@Injectable({
  providedIn: 'root'
})
export class MenuService {

  constructor(private copyService: CopyService) { 
    // something going on with the copyService
  }
  
 }

最后,MenuService 被注入到一个组件中:

export class Component implements OnInit {
  
  constructor(
    private store: Store<fromRoot.State>,
    private menuService: MenuService,    
  ) {
  }
  
  ngOnInit() {
      // something going on with the menuService
  }
}

抱歉,仅提供实际代码的草稿,但我不确定我能实际展示多少。 无论如何,问题是我得到一个 StaticInjectorError(AppModule)[Component -> Store] No provider for Store!

我似乎可以找到问题的根源。 CopyService 当然列在我的应用程序的 MainModule 的提供程序部分。

@NgModule({
  imports: [    
    StoreModule.forRoot(reducers, {})       
  ],
  providers: [CopyService],
  entryComponents: [
    //entry components
  ],
  declarations: [
    //declarations
  ]
})
export class MainModule { }

我正在深入研究 Angular 的依赖注入,但到目前为止,还没有任何方法可以为我指明解决这个问题的正确方向。

提前非常感谢!

【问题讨论】:

    标签: angular dependency-injection store ngrx


    【解决方案1】:

    你应该在你的模块中导入商店。

    https://ngrx.io/guide/store#tutorial

    【讨论】:

    • 嗨,我已经编辑了帖子 - 商店已经导入到我的模块中
    • 如果你有复制品我很乐意看看
    猜你喜欢
    • 2013-12-22
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多