combineReducers搭配依赖EntityAdapter的子reducer时选择器报错如何解决
问题原因
你编写的选择器报错是因为fromStudProfReducer.selectAll是针对StudentProfileState类型生成的实体选择器,它需要接收StudentProfileState作为入参才能正确读取内部的ids、entities属性生成列表。
而你当前的写法直接把整个StudentState(包含studentProfile、studentAdmin两个子切片)传给了selectAll,它读取不到对应属性返回undefined,EntityAdapter内部的selectAll逻辑需要调用ids.map()生成实体数组,就触发了Cannot read property 'map' of undefined报错。
正确写法
你只需要先从Feature根状态中提取出studentProfile子切片,再传给实体选择器即可:
// student-profile.selectors.ts 中新增子切片选择器 export const selectStudentProfileState = createSelector( getStudentsFeatureState, (state) => state.studentProfile ); // 改写全量学生档案选择器,不需要额外写冗余的投影函数 export const selectAllStudentProfiles = createSelector( selectStudentProfileState, fromStudProfReducer.selectAll );
修改后原有facade中的调用代码不需要做任何调整,即可正常获取到全量学生档案数组。
内容的提问来源于stack exchange,提问作者Richard77
相关产品推荐
相关产品推荐

