【发布时间】:2020-02-03 18:17:57
【问题描述】:
我在弄清楚如何在 Angular 中编写分层存储时遇到了问题。我想要多个在应用程序的特定部分工作的减速器。
这是一个描述:这是一个 HR 应用程序。我有人才(员工)模块,其数据分为五个部分:专业知识、教育、背景检查、位置和薪水、工作职位。
每个部分都包含相当复杂的逻辑以满足业务期望。 我们正在为市场更新一些案例,我们需要在多个部分的组件之间共享数据。
所以从根模块开始,我已经为天赋模块定义了根状态
我尝试过使用 combineReducers,如这些帖子中所述: Angular 6 / NGRX Combine Reducers
所以从根模块开始,我已经为天赋模块定义了根状态
export interface ITalentState {
profile: IProfileState;
}
这里是天赋状态的减速器
export function talentReducer(state = talentInitialState, action: TalentActions): ITalentState {
switch(action.type) {
case TalentActionTypes.SetProfile:
return {
...state,
profile: action.profileData
}
default: return state;
}
}
ProfileState 是这样定义的
export interface IProfileState {
expertiseState: IExpertiseState;
}
这里是专业知识部分:
export interface IExpertiseState {
expertiseQuestions: ExpertiseQuestions;
error: string;
}
专业部分
function expertiseReducer(state, action)
switch(action.type) {
case ExpertiseActionsTypes.LoadFailed:
return {
...state,
expertiseQuestions: new ExpertiseQuestions(),
error: action.message
}
case ExpertiseActionsTypes.LoadSuccess:
return {
...state,
expertiseQuestions: action.questions,
error: ''
}
default: return state;
}
}
我希望有一种方法可以实现存储粒度和拆分reducer,以便它们更易于管理,但我在调试器中看到的唯一一件事是调用了专业存储方法,但从不调用reducer,并且值没有更新商店。
【问题讨论】:
-
ngrx 是通向精神错乱的道路,你为什么要选择这样破坏你的项目?
-
应用程序相当复杂,一些数据在多个模块之间共享。存储的第一个原因是我不想打破模块隔离并将其服务公开给其他模块只是为了获取一两条数据。