IF 在 Redux Observable 史诗中

Ste*_*n B 1 observable rxjs redux-observable

我有一个史诗,它捕获每个获取状态的调度(只是来自状态的项目,例如 state.process:{ status: fail, success, inWork},而不是像 200、500 等的请求状态)。当状态 == 成功(通过从状态获取状态)时,我需要调度另一个动作,如 SET_STATUS_SUCCESS

const getStatus = (action, state) =>
    action.pipe(
        ofType(GET_STATUS),
        withLatestFrom(state),
        mergeMap(([action, state]) => {
            const { status } = state.api.process; //here is what i need, there is no problem with status.
            if (status === "success") {
              return mapTo(SET_STATUS_SUCCESS) //got nothing and error.
}
        })
    );
Run Code Online (Sandbox Code Playgroud)

现在我收到错误:

未捕获的类型错误:您提供了 'function (source) { return source.lift(new MapToOperator(value)); }' 需要流的地方。您可以提供 Observable、Promise、Array 或 Iterable。在 subscribeTo (subscribeTo.js:41)

我该怎么办?我尝试只返回 setStatusSuccess 操作,但它也不起作用。

mpo*_*tus 5

您需要从传递给的函数返回一个可观察的对象mergeMap。尝试这个:

const getStatus = (action, state) =>
  action.pipe(
    ofType(GET_STATUS),
    withLatestFrom(state),
    mergeMap(([action, state]) => {
      const { status } = state.api.process;

      if (status === 'success') {
        return of({ type: SET_STATUS_SUCCESS });
      } else {
        return EMPTY;
      }
    }),
  );
Run Code Online (Sandbox Code Playgroud)

ofEMPTY是从rxjs导入的。