【问题标题】:In Redux-Observable must ofType be followed immediately by one of the map operators?在 Redux-Observable 中,ofType 必须紧跟其中一个映射运算符吗?
【发布时间】:2018-08-07 03:43:37
【问题描述】:

我正在下面的史诗中发出 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
          })
        )
      )
    )
  );

【问题讨论】:

    标签: rxjs redux-observable


    【解决方案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
                })
              )
            )
          )
        )
      );
    

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 2022-11-04
      • 2016-03-04
      相关资源
      最近更新 更多