加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 06:52:03 所属栏目:安全 来源:网络整理
导读:我目前正在开发一个简单的测试应用程序,以了解有关@ ngrx / store的更多信息.我有一个名为TrainingModule的模块,它应该存储一些练习和更多信息. 代码有效,但我试着在这里改进.我目前拥有的是我的功能模块,如下所示: @NgModule({ imports: [ CommonModule,Tr
我目前正在开发一个简单的测试应用程序,以了解有关@ ngrx / store的更多信息.我有一个名为TrainingModule的模块,它应该存储一些练习和更多信息.
代码有效,但我试着在这里改进.我目前拥有的是我的功能模块,如下所示:

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

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

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);
  }

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

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

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

export const trainingReducers = {
  'exercise': exerciseReducer
};

StoreModule.forFeature('training',trainingReducers)

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

解决方法

我可以举个例子来说明我是怎么做到的.我使用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);

模块/ user.module.ts

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读