9

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

NullInjectorError:商店没有提供者!

商店已导入,如文档中所示:

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

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

3 回答 3

11

Turned out that some of my services imported the store via

import { Store } from '@ngrx/store/src/store'

Changing the imports to

import { Store } from '@ngrx/store'

fixed the problem.

于 2017-11-06T20:30:24.040 回答
0

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

我的解决方案是:

  1. 在主体中定义一个商店模拟describe
let storeMock;
  1. 在部分初始化它beforeEach
  beforeEach(async () => {
    storeMock = {
      dispatch: jasmine.createSpy("dispatch"),
      pipe: jasmine.createSpy("pipe").and.returnValue(from([{
...
        requestTimeout: 5000,
...
      }]))
    };
  1. 为 Store in 定义提供者TestBed.configureTestingModule
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
      ],
      providers: [
        ...
        {
          provide: Store,
          useValue: storeMock
        }
        ...
      ]
    });
    ```
于 2019-07-02T10:48:03.933 回答
0

对于 ngrx 8 使用:

import { provideMockStore } from '@ngrx/store/testing';
于 2020-02-03T14:33:52.213 回答