gpt4 book ai didi

ngrx - Ngrx:结合两个选择器

转载 作者:行者123 更新时间:2023-12-04 13:15:47 27 4
gpt4 key购买 nike

我已经建立了这个IStore

export interface IStore {
user: IUser;
sources: ISourceRedux;
}


其中 IUser是:

export interface IUser {
id: string;
cname: string;
sname: string;
...
}


ISourceRedux是:

export interface ISourceRedux {
entities: { [key: string]: ISource };
ids: Array<string>;
selectedIds: Array<string>;
editingSource: ISource;
defaultId: string;
}


因此,我创建了以下选择器:

export const getSourcesState = (state: IStore) => state.sources;
export const getSelectedIds = (sourceRdx: ISourceRedux) => sourceRdx.selectedIds;
export const getSelectedSourceIds = createSelector(getSourcesState, fromSources.getSelectedIds);


因此,到目前为止,为了检查是否已登录用户,我这样做了:

this.store$
.select(fromRoot.getUserState)
.filter(user => user.id != null && user.logged)
.do(user => this.store$.dispatch(...))
...


现在,我正在努力获取用户信息和selectedSourceIds,以便检查是否:


用户已登录( this.store$.select(fromRoot.getUserState)
然后获取所有selectedSourceIds( this.store.select(fromRoot.getSelectedSourceIds)
采取行动


我怎么能得到这个?

最佳答案

将该代码添加到选择器是否有意义:

// Selector functions
const getProductFeatureState = createFeatureSelector<ProductState>('products');
const getUserFeatureState = createFeatureSelector<UserState>('users');

export const getCurrentProduct = createSelector(
getProductFeatureState,
getUserFeatureState,
getCurrentProductId,
(state, user, currentProductId) => {
if (currentProductId === 0) {
return {
id: 0,
productName: '',
productCode: 'New',
description: 'New product from user ' + user.currentUser,
starRating: 0
};
} else {
return currentProductId ? state.products.find(p => p.id === currentProductId) : null;
}
}
);


此代码在product.reducer文件中。在这里,我为产品和用户定义了功能选择器。

然后,我同时使用产品和用户功能构建一个 getCurrentProduct选择器。

关于ngrx - Ngrx:结合两个选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43257027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com