@ ngrx/store结合了功能模块中的多个reducer

jhe*_*hen 10 ngrx angular ngrx-store-4.0

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

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

和我的减速机看起来像那样:

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);
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.在我的模板中,我从商店中选择我的练习

exercise$: Observable<IExercise[]>;

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

  ngOnInit() {
    this.exercise$ = this.store.select(getExercises);
  }
Run Code Online (Sandbox Code Playgroud)

所以我现在要做的就是将我的减速器组合起来,这样我就不必像这样添加每个减速器了

StoreModule.forFeature('exercises', exerciseReducer);
StoreModule.forFeature('sample', sampleReducer);
StoreModule.forFeature('sample1', sampleReducer1);
Run Code Online (Sandbox Code Playgroud)

在我的所有模块中.我试图收集所有减速器

export const trainingReducers = {
  'exercise': exerciseReducer
};
Run Code Online (Sandbox Code Playgroud)

StoreModule.forFeature('training', trainingReducers)
Run Code Online (Sandbox Code Playgroud)

但这让我无法在控制台中读取未定义错误消息的属性"练习".也许有人可以帮助我理解,我如何从功能模块中收集所有减速器并为其创建正确的选择器.

whe*_*ler 19

我可以举个例子来说明我是怎么做到的.我使用index.ts来捆绑模块中的所有其他reducer,如下所示:

模块/减速器/ 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);
Run Code Online (Sandbox Code Playgroud)

模块/ user.module.ts

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

  • @Phil我更新了答案并添加了一些选择器。 (2认同)
  • @NicoleZ您可以通过向其添加功能部件/模块状态来扩展根状态。根状态(可以为空)是每个功能部件/模块都知道并且必须扩展的状态。这有帮助吗? (2认同)