比较来自世界各地的卖家的域名和 IT 服务价格

ngrx: 如何在方法内传输选择器参数 createSelector

我有一个非常简单的条件:


const state = {
records: [1,2,3],
};


我有一个选择器:


export const getRecords = createSelector/getState, /state: State/ => state.records//;


现在我希望有单独的选择器来提取索引上的每个条目。
为此目的,我想以这种方式为细节创建一个通用选择器:


export const getRecordByIndex = createSelector/
getRecords,
/state: State, { index }/ => state.records[index]/,
/;


然后创建一对特定的选择器,例如:


export const getFirstRecord = createSelector/
getRecordByIndex//* somehow pass index = 0 to this selector *//,
/firstRecord/ => firstRecord/,
/;


但是当我们在方法内使用它们时,我没有找到如何将选择器传输到有关道具的选择器 createSelector. 是否有可能?
已邀请:

莫问

赞同来自:

来自这个博客文章:
https://medium.com/angular-in- ... 529f8
以。。。开始 NgRx 6.1 选择器也接受其他参数 props. 哪一个
这意味着现在您可以确定选择器如下:


export const getCount = createSelector/
getCounterValue,
/counter, props/ => counter * props.multiply
/;

this.counter = this.store.pipe/
select/fromRoot.getCount, { multiply: 2 }/
/;


兄弟 ... 但重写您的问题,您可以询问如何构建其他使用此选择器的选择器? 上述文章建议建立工厂职能。

诸葛浮云

赞同来自:

我用
"@ngrx/entity": "7.2.0",

并且我看到道具被传送到每个选择器,例如,在我的组件中,我因果关系:


this.isActive$ = this.store.pipe/select/fromClient.isActive, { id: 'someid' }//;


然后在我的变速箱中我有以下几个:


export const getClientState = createFeatureSelector<clientstate>/'client'/;

export const getClient = createSelector/
getClientState,
/state, props/ =&gt; {
// do something with props.id to get the client then:
return state;
}
/;

export const isActive: = createSelector/
getClient, // props are passed to here
/state: any/ =&gt; { // i don't add the props argument here, as i don't need them
return state.isActive;
}
/;


</clientstate>

卫东

赞同来自:

您可以使用投影机功能:


export interface Record {
// Some sort of record interface
}

export interface State {
records: Record[];
}

export const getRecords = createSelector/
getState,
/state: State/: Record[] => state.records/
/;

export const getRecordByIndex = createSelector/
getRecords,
/records: Record[], { index }/ => records[index]/,
/;

export const getFirstRecord = createSelector/
getRecords,
/records: Record[]/ => getRecordByIndex.projector/records, { index: 0 }/
/;

要回复问题请先登录注册