在 Redux-Observable 中,ofType 必须紧跟地图运算符之一吗?
In Redux-Observable must ofType be followed immediately by one of the map operators?
我正在下面的史诗中发出 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
})
)
)
)
);
您可以使用 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
})
)
)
)
)
);
我正在下面的史诗中发出 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
})
)
)
)
);
您可以使用 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
})
)
)
)
)
);