NgRx 如何订阅特性模块的变化?
How to subscribe to changes in feature module in NgRx?
我正在使用 AoT
、and lazy-loading
。我也在尝试延迟加载我的商店模块。我的问题是,当我订阅功能模块时,我得到 'undefined' 而不是获取商店数据,使用 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
};
然后我将它注入到我的主应用程序模块中,如下所示,
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);
});
我做错了什么?
通过将 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 的组件中,您 select 功能 select 或者如下所示,
this.appStore
.select(fromApp.getRegistryState)
.map(data => data.patientRegistration)
.subscribe(data => {
if (data.loaded) {
this.patientRestration.patchValue(data.patientData);
console.log(data.patientData);
}
});
我正在使用 AoT
、and lazy-loading
。我也在尝试延迟加载我的商店模块。我的问题是,当我订阅功能模块时,我得到 'undefined' 而不是获取商店数据,使用 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
};
然后我将它注入到我的主应用程序模块中,如下所示,
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);
});
我做错了什么?
通过将 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 的组件中,您 select 功能 select 或者如下所示,
this.appStore
.select(fromApp.getRegistryState)
.map(data => data.patientRegistration)
.subscribe(data => {
if (data.loaded) {
this.patientRestration.patchValue(data.patientData);
console.log(data.patientData);
}
});