【问题标题】:Using NGRX within Angular Library在 Angular 库中使用 NGRX
【发布时间】:2021-02-02 01:47:24
【问题描述】:

我在我的应用程序中使用 NGRX。但是在我的 Angular 库中,我想加入那个 NGRX 模块并添加到它。但是,当我尝试在图书馆内拨打StoreModule.forRoot() 时,我不能。因为我已经在主应用程序中调用过一次。

  imports: [
    CommonModule,
    IonicModule,
    CompoLoginRoutingModule,
    ReactiveFormsModule,
    TranslateModule.forRoot({
      loader: {
        deps: [HttpClient],
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
      },
    }),
    StoreModule.forRoot(reducers, { // This is a problem
      runtimeChecks: {
        strictStateImmutability: true,
        strictActionImmutability: true,
      },
      metaReducers,
    }),

有没有正确的方法加入主应用的NGRX?

【问题讨论】:

    标签: angular ngrx ngrx-store


    【解决方案1】:

    如果你在 app 模块中使用了 ngrx store 模块,那么请在 app.moudle.ts 中添加这个部分

    import { reducers, metaReducers } from "./reducers";
    
    StoreModule.forRoot(reducers, {
          metaReducers,
          runtimeChecks: {
            strictStateImmutability: true,
            strictActionImmutability: true,
          },
        }),
    

    这是生成根存储的命令

    ng generate store AppState --root --module app.module.ts
    

    app.module.ts 中也有这样的效果

    EffectsModule.forRoot([AppEffects]),
    

    如果 ngrx 将模块存储在子模块中,则添加这样的 assets.moudle.ts,

    import * as fromAssetsStore from "./store/assets.reducer";
    
    StoreModule.forFeature(
      fromAssetsStore.assetsFeatureKey,
      fromAssetsStore.aReducer,
      {
        metaReducers: fromAssetsStore.metaReducers,
      }
    ),
    EffectsModule.forFeature([AssetsEffects]),
    

    【讨论】:

      猜你喜欢
      • 2019-02-11
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 2022-06-14
      • 2020-04-11
      • 2020-03-27
      相关资源
      最近更新 更多