【问题标题】:Error: No Provider for Store! in @ngrx 4.x错误:商店没有提供者!在@ngrx 4.x
【发布时间】:2018-04-19 02:32:53
【问题描述】:

在将我的项目从 @ngrx 2.x 迁移到 4.1.0 时,我遇到了错误消息

NullInjectorError: 没有 Store 提供者!

店铺导入如docs:

import { StoreModule as NgRxStoreModule } from '@ngrx/store';

@NgModule({
  imports: [
    NgRxStoreModule.forRoot(reducerMap, {
      initialState: initial
    }),
    StoreRouterConnectingModule,
    EffectsModule.forRoot(effects)
  ],
  providers: [AppActions]
})
export class StoreModule {}

【问题讨论】:

    标签: angular ngrx ngrx-store ngrx-store-4.0


    【解决方案1】:

    原来我的一些服务通过

    导入了商店
    import { Store } from '@ngrx/store/src/store'
    

    将导入更改为

    import { Store } from '@ngrx/store'
    

    解决了问题。

    【讨论】:

    • 自动导入:@
    • 我在 Web Storm 中添加“@ngrx/store/store”时遇到了类似的自动导入问题
    【解决方案2】:

    我在尝试以 Angular 7 运行测试时得到了这个。

    我的解决方案是:

    1. describe 的正文中定义一个 store mock:
    let storeMock;
    
    1. beforeEach部分初始化它:
      beforeEach(async () => {
        storeMock = {
          dispatch: jasmine.createSpy("dispatch"),
          pipe: jasmine.createSpy("pipe").and.returnValue(from([{
    ...
            requestTimeout: 5000,
    ...
          }]))
        };
    
    1. TestBed.configureTestingModule 中定义Store 的提供者:
        TestBed.configureTestingModule({
          imports: [
            HttpClientTestingModule,
          ],
          providers: [
            ...
            {
              provide: Store,
              useValue: storeMock
            }
            ...
          ]
        });
        ```
    

    【讨论】:

      【解决方案3】:

      对于 ngrx 8 使用:

      import { provideMockStore } from '@ngrx/store/testing';
      

      【讨论】:

        猜你喜欢
        • 2018-03-23
        • 2017-10-31
        • 2018-12-06
        • 1970-01-01
        • 2020-06-26
        • 1970-01-01
        • 2018-01-16
        • 2018-04-03
        • 2018-03-16
        相关资源
        最近更新 更多