NGRX:结合到选择

问题描述:

我已经建立了这个IStoreNGRX:结合到选择

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(...)) 
    ... 

现在我strugling用于获取用户信息和selectedSourceIds在同一时间,以便检查如果:

  1. 用户登录(this.store$.select(fromRoot.getUserState)
  2. 然后让所有selectedSourceIds(this.store.select(fromRoot.getSelectedSourceIds)
  3. 调度一个动作

我怎么能得到这个?

+0

看看'withLatestFrom' RxJs运营商 – Maxime

这是我的解决方案:

this.store$.combineLatest(
     this.store$.select(fromRoot.getUserEntity), 
     this.store$.select(fromRoot.getSelectedSourceIds), 
     (store, user, selectedSourceIds) => ({user: user, selectedSourceIds: selectedSourceIds}) 
    ) 
    .filter((proj) => proj.user.id != null && proj.user.logged) 
    .do((proj) => this.store$.dispatch({type: 'DELETE_CARDS', payload: {username: proj.user.username, tokens: proj.selectedSourceIds}})) 
    .take(1) 
    .subscribe(); 

我希望它是有用的。

+0

我认为这种方法没有利用重新选择(memoized选择器)的解释与此处的结果https://netbasal.com/lets-talk-about-select-and -reselect功能于NGRX店,177a2f6045a8 –