在Redux-Observable中,ofType是否必须紧跟在地图运算符之一之后?

时间:2018-08-06 11:53:20

标签: rxjs redux-observable

我正在下面的史诗中发出ajax请求,但是在mergeMap开始调用之前,我想触发RESET_IMAGE操作,但是,下面的代码无法实现。有可能还是有人可以看到我在做什么错了?

const imageUploadEpic = (action$, state$) =>
  action$.pipe(
    ofType('UPLOAD_IMAGE'),
    mapTo('RESET_IMAGE'),
    mergeMap(action =>
      from(
        axios.post(`/uploads/url`, {
          url: action.src
        })
      ).pipe(
        map(response => ({
          type: 'UPLOAD_IMAGE_SUCCESS',
          data: response.data
        })),
        catchError(error =>
          of({
            type: 'UPLOAD_IMAGE_ERROR',
            error
          })
        )
      )
    )
  );

1 个答案:

答案 0 :(得分:1)

您可以使用concat来产生两个这样的动作:

const imageUploadEpic = (action$, state$) =>
  action$.pipe(
    ofType('UPLOAD_IMAGE'),
    mergeMap(action =>
      concat(
        of({ type: 'RESET_IMAGE' }),
        from(
          axios.post(`/uploads/url`, {
            url: action.src
          })
        ).pipe(
          map(response => ({
            type: 'UPLOAD_IMAGE_SUCCESS',
            data: response.data
          })),
          catchError(error =>
            of({
              type: 'UPLOAD_IMAGE_ERROR',
              error
            })
          )
        )
      )
    )
  );