NgRx父选择器静态参数

时间:2020-08-05 15:01:19

标签: angular ngrx

假设我有接收道具的选择器:

export const selectTaskById = createSelector(
  selectCoreState,
  (state, props: { id: TaskKey }) => state?.tasks[props.id] || null
);

然后我有另一个选择器,它不接收道具,但是它需要通过提供硬编码的id来使用选择器上方的数据安全性:

export const selectGeneralTask = createSelector(
  selectTaskById, // -- Here I want to pass hardcoded props with {id: 'generalTask'}
  (state) => state
);

我在SO和Internet上进行了搜索,但没有找到解决方案。

最重要的是,我想找到一种方法,如何在选择器上进行某种“抽象”,而无需在需要特定任务信息的每个地方都知道“ id”。

更新
就目前而言,我发现的唯一解决方案如下:
我又添加了一个选择器:

export const selectRootState = (state: AppState) => state;

然后在selectGeneralTask中使用它:

export const selectGeneralTask = createSelector(
  selectRootState,
  (state) => selectTaskById(state, {id: 'generalTask'})
);

如果有人有其他解决方法,我会很高兴听到它。

1 个答案:

答案 0 :(得分:1)

您可以创建工厂选择器:

export const selectTaskById = (id) => createSelector(
  selectCoreState,
  (state) => state?.tasks[id] || null
);

export const selectGeneralTask = createSelector(
  selectTaskById('generalTask'),
  (state) => state
);
相关问题