@ngrx/store combine multiple reducers from feature module

后端 未结 2 1844
逝去的感伤
逝去的感伤 2021-02-01 08:52

I am currently working on a simple test app to learn more about the @ngrx/store. I have a module called TrainingModule which should store some exercises and more information. T

2条回答
  •  天涯浪人
    2021-02-01 09:21

    I can give you an example how I did it. I used an index.ts to bundle all other reducers from within the module like this:

    module/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('userModule');
    
    
    export const selectSearchState = createSelector(
        selectUserModuleState, (state: UserModuleState) => state.search
    );
    export const getSearchLoading = createSelector(selectSearchState, fromSearch.getLoading);
    export const getSearchEntities = createSelector(selectSearchState, fromSearch.getEntities);
    

    module/user.module.ts

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

提交回复
热议问题