【发布时间】:2022-01-20 01:08:28
【问题描述】:
由于 NGRX deprecated selectors with props 在版本 11 中。使用属性的预期方法是创建工厂选择器,
- 如何嵌套选择器,或者从另一个调用一个并在它们之间传递状态?
改变之前,有以下两个选择器
export const selector1 = createSelector(
state,
( state: FormState, props: {id: string} ) => {
// Return items whose parent match the given id
return state.items.filter( item => item.parentId === props.id);
}
);
export const selector2 = createSelector(
state
( state, FormState, props: { id: string} ) => {
return state.notes.filter( note => note.parentId === props.id);
}
)
您可以从另一个选择器中调用其中一个,如下所示
export const selector3 = createSelector(
state,
( state: FormState, props: {id: string} ) => {
// get notes by using an existing selector and passing the state & properties
const notes = selector2({ storeName: state}, props)
// do some more logic based on the nested call to a selector
...
}
);
现在工厂选择器是处理属性时的预期格式,选择器现在看起来像下面这样
export const selector1 = (id: string) => createSelector(
state,
( state: FormState ) => {
// Return items whose parent match the given id
return state.items.filter( item => item.parentId === id);
}
);
export const selector2 = (id: string) => createSelector(
state
( state, FormState ) => {
return state.notes.filter( note => note.parentId === id);
}
)
- 给定工厂选择器,有没有办法从
selector1中调用selector2 - 如果是,状态是如何传递给嵌套选择器的
例如
export const selector3 = (id: string) => createSelector(
state,
( state: FormState ) => {
// how is the `state` passed to the nested selector call below?
const notes = selector2( id)
}
);
谢谢。
【问题讨论】:
标签: angular ngrx ngrx-selectors