angular ngrx存储参数化的备注选择器

时间:2019-05-21 14:51:04

标签: angular rxjs ngrx-effects

我读了这篇文章https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8

未找到如何适应将参数传递给选择器的方法

我尝试将props对象传递到组件中的选择器。

使用@JuliusDzidzevičius解决方案进行[编辑]答案。

在组件中

this.store.pipe(select(fromRoot.selectors.getPreferences, 'myProps'))

在AppState

const rootSelectors = {
  layout: (state: AppState) => _.get(state, 'layout'),
}

export interface Selectors {
  getPreferences: MemoizedSelectorWithProps<AppState, string, Preference[]>;
}

export const selectors: Selectors = {
  getPreferences: createSelector(
      rootSelectors.layout,
      (state: LayoutState, props: string) 
=> layoutSelectors.preferences(state, props)) 
};

状态

export const layoutSelectors = {
  preferences: (state: LayoutState, props: string) => {
    return state.filter(item => item.name === props)
  },
};

如果有人可以告诉我如何正确调整此参数。通过记忆的选择器从组件到选择器。

2 个答案:

答案 0 :(得分:0)

您可以这样做

export const getUserById = (userId: string) => createSelector(
    getUserList,
    idList => IdList.filter(id => id === userId)
);

如果您仍有问题,请告诉我

答案 1 :(得分:0)

已记忆的选择器参数保存在props属性下。在这里:

(counter) // should be somewhere else but don't know how :(
=> layoutSelectors.preferences(counter))

counter是来自rootSelectors.layout的值。无法说出其背后的内容或您的目标是什么,而是要在选择器中访问道具:

(counter, props) => layoutSelectors.preferences(props.counter))