ngrx参数选择功能 [英] ngrx parameter to select function
问题描述
是否可以将参数传递给ngrx select函数?
Is there a way to pass parameters to ngrx select functions?
以下是我的用例:
我正在维护商店中的评论列表.
I am maintaining list of comments in store.
我写了一个组件来代表一个评论.因此,一个CommentComponent知道组件对象的ID
I wrote one component to represent one comment. So, one CommentComponent knows the id of the component object
每个评论将具有likeByBy,reportedBy等属性. 在用户界面中,我正在使用* ngFor
Each comment will have properties like likedBy, reportedBy, ...etc In UI, I am showing all the components using *ngFor
现在,我希望我的CommentComponent通过使用组件的ID仅订阅一个注释对象.
Now I want my CommentComponent to subscribe only to one comment object by using id of the component.
现在,我在顶级组件中订阅所有评论,并将每个评论作为输入传递给CommentCompoent.
Right now I am subscribing to all the comments in a top level component, and passing each comment to the CommentCompoent as an input.
当前的方法并不干净,因为即使只有一个注释发生更改,角度变化检测策略(即使我使用onPush)也必须为所有注释呈现DOM.
Current approach is not clean because angular change detection strategy (even if I use onPush) has to render DOM for all the comments even though only one comment changes.
赞赏任何可以在其中将注释ID传递给选择器功能的建议,以便每个CommentComponent只能订阅一个注释.
Appreciate any suggestions where I can pass comment id to a selector function so that each CommentComponent can subscribe to only one comment.
预先感谢, 苏达喀尔
推荐答案
从NgRx 6.1开始,您可以将选择器与props一起使用.
As of NgRx 6.1, you can use a selector with props.
export const getCount = () =>
createSelector(
(state, props) => state.counter[props.id],
(counter, props) => counter * props.multiply
);
有关更多信息和不同方式,请查看我的文章 NgRx:参数化选择器
For more info and different ways, check out my article NgRx: Parameterized selectors
这篇关于ngrx参数选择功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!