gpt4 book ai didi

angular - @ngrx/store 组合来自功能模块的多个 reducer

转载 作者:太空狗 更新时间:2023-10-29 17:08:54 25 4
gpt4 key购买 nike

我目前正在开发一个简单的测试应用程序,以了解有关@ngrx/store 的更多信息。我有一个名为 TrainingModule 的模块,它应该存储一些练习和更多信息。代码有效,但我尝试在这里改进。我目前拥有的是我的功能模块,如下所示:

@NgModule({
imports: [
CommonModule,
TrainingRoutingModule,
StoreModule.forFeature('exercises', exerciseReducer)
],
declarations: [
TrainingDashboardComponent,
TrainingCoreComponent,
TrainingNavComponent,
TrainingPlanComponent,
ExerciseOverviewComponent,
ExerciseListComponent]
})
export class TrainingModule {
}

我的 reducer 看起来像这样:

export interface ExerciseState {
exercises: IExercise[];
}

export interface State extends fromRoot.State {
'exercises': ExerciseState;
}

export const initialState: ExerciseState = {
exercises: [
{id: 1, name: 'Exc 1'},
{id: 2, name: 'Exc 2'}
]
};

export function exerciseReducer(state: ExerciseState = initialState, action: any): ExerciseState {
switch (action.type) {
default:
return state;
}
}

export const getExerciseState = createFeatureSelector<ExerciseState>('exercises');
export const getExercises = createSelector(getExerciseState, state => state.exercises);

到目前为止一切顺利。在我的模板中,我从商店中选择了我的练习

exercise$: Observable<IExercise[]>;

constructor(private store: Store<State>) { }

ngOnInit() {
this.exercise$ = this.store.select(getExercises);
}

所以我现在想做的是组合我的 reducer ,这样我就不必像这样添加每个 reducer

StoreModule.forFeature('exercises', exerciseReducer);
StoreModule.forFeature('sample', sampleReducer);
StoreModule.forFeature('sample1', sampleReducer1);

在我所有的模块中。我试图用

收集所有 reducer
export const trainingReducers = {
'exercise': exerciseReducer
};

StoreModule.forFeature('training', trainingReducers)

但这给了我一个 Cannot read property 'exercises' of undefined 错误消息。也许有人可以帮助我理解,我如何从功能模块中收集所有 reducer 并为此创建正确的选择器。

最佳答案

我可以举例说明我是如何做到的。我使用 index.ts 从模块中捆绑所有其他 reducer ,如下所示:

模块/reducers/index.ts

import * as fromRoot from '../../../reducers';
import * as fromSearch from './search';
import * as fromUserDetail from './user-detail';
import * as fromDetailBase from './base';


export interface UserModuleState {
search: fromSearch.State;
detail: fromUserDetail.State;
detailBase: fromDetailBase.State;
}

export interface State extends fromRoot.State {
userModule: UserModuleState;
}

export const reducers = {
search: fromSearch.reducer,
detail: fromUserDetail.reducer,
detailBase : fromDetailBase.reducer
};

export const selectUserModuleState = createFeatureSelector<UserModuleState>('userModule');


export const selectSearchState = createSelector(
selectUserModuleState, (state: UserModuleState) => state.search
);
export const getSearchLoading = createSelector(selectSearchState, fromSearch.getLoading);
export const getSearchEntities = createSelector(selectSearchState, fromSearch.getEntities);

模块/user.module.ts

import { reducers } from './reducers';
@NgModule({
imports: [
...
StoreModule.forFeature('userModule', reducers)
],
...
})
export default class UserModule { }

关于angular - @ngrx/store 组合来自功能模块的多个 reducer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47052282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com