如何订阅NgRx中功能模块的更改?

时间:2018-03-08 03:57:13

标签: angular lazy-loading ngrx ngrx-store

我正在使用AoTand lazy-loading。我也试图延迟加载我的商店模块。我的问题是,当我订阅功能模块时,我得到了“未定义的”#39;而不是使用Redux开发工具获取商店数据,我可以看到该功能被延迟加载并正确填充数据。

这是我的appState reducer,

export interface AppState {
  auth: fromAuth.State;
  httpAction: fromHttpActions.State;
  Geo: fromGeoActions.State;
  UI: fromUI.State;
  DbSearch: fromDbSearch.State;
  LocationSearch: fromlocationSearchReducer.State;
  AppDropdownList: fromdropdownReducer.State;
}

export const reducers: ActionReducerMap<AppState> = {
  auth: authReducer,
  httpAction: httpActionReducer,
  Geo: geoReducer,
  UI: userInterfaceReducer,
  DbSearch: dbSearchReducer,
  LocationSearch: locationSearchReducer,
  AppDropdownList: dropdownReducer
};

我将其注入我的主app-module中,如下所示,

StoreModule.forRoot(reducers),

以上模块运行正常,

问题在于以下内容,

export interface IRegistryState {
  patientRegistration: frompatientRegistrationReducer.State;
}
export const registryReducers: ActionReducerMap<IRegistryState> = {
  patientRegistration: patientRegistrationReducer
};

我将它注入功能模块中,如下所示,

StoreModule.forFeature('registryStore', registryReducers),

当我执行以下操作时,返回的值始终未定义

  this.registryStore.select(s => s.patientRegistration).subscribe(data => {
      console.log(data);
    });

我在这里做错了什么?

enter image description here

1 个答案:

答案 0 :(得分:1)

通过将createFeatureSelector添加到我的AppState模块const getRegistryState来解决问题。 所以在创建功能存储模块后,我需要将以下魅力添加到我的AppStore模块,

export interface AppState {
  auth: fromAuth.State;
  httpAction: fromHttpActions.State;
  Geo: fromGeoActions.State;
  UI: fromUI.State;
  DbSearch: fromDbSearch.State;
  LocationSearch: fromlocationSearchReducer.State;
  AppDropdownList: fromdropdownReducer.State;
}

export const reducers: ActionReducerMap<AppState> = {
  auth: authReducer,
  httpAction: httpActionReducer,
  Geo: geoReducer,
  UI: userInterfaceReducer,
  DbSearch: dbSearchReducer,
  LocationSearch: locationSearchReducer,
  AppDropdownList: dropdownReducer
};
export const getRegistryState = createFeatureSelector<IRegistryState>(
  'registryStore'
);

并在组件中仍然调用AppState Store并选择功能选择器,如下所示,

this.appStore
          .select(fromApp.getRegistryState)
          .map(data => data.patientRegistration)
          .subscribe(data => {
            if (data.loaded) {
              this.patientRestration.patchValue(data.patientData);
              console.log(data.patientData);
            }
          });