我有一个史诗,可以捕获每次获取状态的调度(只是来自状态的项目,例如 state.process:{ status: failed, 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.
}
})
);
现在我收到错误:
Uncaught TypeError: You provided 'function (source) { return source.lift(new MapToOperator(value)); }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at subscribeTo (subscribeTo.js:41)
我该怎么办?我尝试只返回 setStatusSuccess 操作,但它也不起作用。
最佳答案
您需要从传递给 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;
}
}),
);
关于rxjs - Redux Observable 史诗中的 IF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53374557/