具有效果在不同情况下会发出不同的动作

时间:2019-07-05 06:24:36

标签: angular redux rxjs ngrx

我有以下效果

  @Effect()
  assignMission$ = this.actions$.pipe(
    ofType<featureActions.AssignUAVMissionRequest>(featureActions.ActionTypes.AssignUAVMissionRequest),
    concatMap(action =>
      of(action).pipe(
        withLatestFrom(this.store$.pipe(select(RoutesStoreSelectors.getById(), {routeId: action.payload.routeId}))),
      )
    ),
    switchMap(([action, route]) => {
      return this.dataService.assignUAVMission(action.payload.params).pipe(
        mergeMap(response => {
          if (route.saveState === RoutesStoreModels.IRouteSaveState.DRAWED) {
            return [
              new RoutesStoreActions.OverrideRoute(),
              new featureActions.AssignUAVMissionSuccess()
            ];
          } else {
            const newRouteId = uuid();
            return [
              new RoutesStoreActions.AddRoute(),
              new featureActions.AssignUAVMissionSuccess()
            ];
          }
        }),
        catchError((error: HttpErrorResponse) => {
          return of(new featureActions.AssignUAVMissionFailed({error}));
        }),
      );
    })
  );

在一种情况下,我想调度OverrideRouteAssignUAVMissionSuccess 在另一种情况下,我想分派AddRouteAssignUAVMissionSuccess

问题是,我的mergeMap返回错误

  src / app / stores / uavs-store / effects.ts(218,18)中的

ERROR:错误TS2345:   类型'(响应:AssignMissionResponse)=>的参数   (AssignUAVMissionSuccess | OverrideRoute)[] | (AssignUAVMission成功   | AddRoute)[]'不能分配给'(value:   AssignMissionResponse,索引:数字)=>   ObservableInput”。

但是我不完全知道该如何解决?

是否可以让效果分配不同的动作集?

1 个答案:

答案 0 :(得分:1)

您可以通过返回像这样的Action类型的数组来解决它-

mergeMap(response => {

          const actions = new Array<Action>();
          if (route.saveState === RoutesStoreModels.IRouteSaveState.DRAWED) {
          actions.push(new RoutesStoreActions.OverrideRoute());
           actions.push(
              new featureActions.AssignUAVMissionSuccess()
            );
           return actions;
          } else {
            const newRouteId = uuid();
           actions.push(new RoutesStoreActions.AddRoute());
           actions.push(
              new featureActions.AssignUAVMissionSuccess()
            );
            return actions;
          }
        })