作用中的有效载荷

时间:2019-07-01 02:35:23

标签: angular redux ngrx

我有以下代码

  @Effect()
  executeMission$ = this.actions$.pipe(
    ofType<featureActions.ExecuteUsedDroneMissionRequest>(featureActions.ActionTypes.ExecuteUsedDroneMissionRequest),
    withLatestFrom(this.store$.pipe(select(MissionsStoreSelectors.selectAllEntities))),
    switchMap(([action, missions]) => {
      const mission = missions[action.payload.missionId] 
      //....
    })
  );

现在,我有一个选择器,应该将其删除=>           const mission = missions [action.payload.missionId]

export const selectAllEntities: (state: object) => Dictionary<IMission> = featureAdapter.getSelectors(selectMissionState).selectEntities;
export const getById = () => createSelector(
  selectAllEntities,
  (entities, props) => entities[props.routeId]
);

但是要使用它,我需要像这样将动作有效负载添加到withLatestFrom中

withLatestFrom(this.store$.pipe(select(MissionsStoreSelectors.selectById(), { missionId: action.payload.missionId }))),

但是在switchMap之外无法执行操作。

有可能这样做吗?

3 个答案:

答案 0 :(得分:2)

使用concatMap,此示例来自我的帖子Start using ngrx/effects for this

 concatMap(action =>  
    of(action).pipe(
      withLatestFrom(store.pipe(select(getUserName)))
    )
  ),

答案 1 :(得分:1)

 @Effect()
  executeMission$ = this.actions$.pipe(
    ofType<featureActions.ExecuteUsedDroneMissionRequest>(featureActions.ActionTypes.ExecuteUsedDroneMissionRequest),
    withLatestFrom(
       this.store$.pipe(select(MissionsStoreSelectors.selectAllEntities)),
       (action, missions) => { mission: missions[action.payload.missionId] }
  ),

类似的东西吗?

答案 2 :(得分:1)

使用switchMap切换到withLatestFrom返回的新可观察对象的流,如下所示:

@Effect()
  executeMission$ = this.actions$.pipe(
    ofType<featureActions.ExecuteUsedDroneMissionRequest>(
      featureActions.ActionTypes.ExecuteUsedDroneMissionRequest
    ),
    switchMap(action => {
      return withLatestFrom(this.store$.pipe(select(MissionsStoreSelectors.selectById(), { missionId: action.payload.missionId })))
    })
  );

因此,您将有效负载作为输入以在选择器中使用它