Redux RXJ一个接一个地进行可观察的分派(不是concat)
我使用的是redux,react中可以观察到的redux 我有以下几点Redux RXJ一个接一个地进行可观察的分派(不是concat),redux,rxjs,observable,redux-observable,Redux,Rxjs,Observable,Redux Observable,我使用的是redux,react中可以观察到的redux 我有以下几点 const goToItemEpic = (action$, state$): Observable<any> => action$.pipe( ofType(ItemsDetailsActions.goToItem), concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}
const goToItemEpic = (action$, state$): Observable<any> => action$.pipe(
ofType(ItemsDetailsActions.goToItem),
concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}) =>
of(ProjectActions.setCurrentProjectId({projectId: action.payload.projectId})).pipe(
delay(1000),
switchMap(() => {
return [
DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId}),
ItemsActions.setCurrentItemId({ itemId: action.payload.itemId })
];
})
)
)
);
我如何在redux observable中做到这一点
编辑:
const goToItemEpic = (action$, state$): Observable<any> => action$.pipe(
ofType(ItemsDetailsActions.goToItem),
concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}) =>
concat(
ProjectActions.setCurrentProjectId({projectId: action.payload.projectId}),
action$.pipe(
ofType(DatastoreActions.getDatastoresSuccess),
first(),
switchMap(() => of(
DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId}),
ItemsActions.setCurrentItemId({ itemId: action.payload.itemId })
))
)
)
)
);
const goToItemEpic=(action$,state$):Observable=>action$.pipe(
类型(ItemsDetailsActions.goToItem),
concatMap((操作:{payload:{projectId:string;datastoreId:string;itemId:string})=>
海螺(
ProjectActions.setCurrentProjectId({projectId:action.payload.projectId}),
动作$.pipe(
OF类型(DatastoreActions.GetDatastoresAccess),
第一个(),
开关映射(()=>of(
DatastoreActions.setCurrentDatastoreId({datastoreId:action.payload.datastoreId}),
ItemsActions.setCurrentItemId({itemId:action.payload.itemId})
))
)
)
)
);
subscribeTo.js:23未捕获类型错误:您提供了无效的对象
预计会有小溪的地方。你可以提供一个可观察的承诺,
数组,或Iterable
您可以再次使用
action$.pipe
等待所需的操作:
const goToItemEpic = (action$, state$): Observable<any> => action$.pipe(
ofType(ItemsDetailsActions.goToItem),
concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}) =>
concat(
of(ProjectActions.setCurrentProjectId({projectId: action.payload.projectId})),
action$.pipe(
ofType(ItemsDetailsActions.getDatastoresSuccess),
first(),
switchMap(() => of(
DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId}),
ItemsActions.setCurrentItemId({ itemId: action.payload.itemId })
)
})
const goToItemEpic=(action$,state$):Observable=>action$.pipe(
类型(ItemsDetailsActions.goToItem),
concatMap((操作:{payload:{projectId:string;datastoreId:string;itemId:string})=>
海螺(
of(ProjectActions.setCurrentProjectId({projectId:action.payload.projectId})),
动作$.pipe(
类型(ItemsDetailsActions.GetDatastoresAccess),
第一个(),
开关映射(()=>of(
DatastoreActions.setCurrentDatastoreId({datastoreId:action.payload.datastoreId}),
ItemsActions.setCurrentItemId({itemId:action.payload.itemId})
)
})
因此,在concat
中,首先发出ProjectActions.setCurrentProjectId
操作,然后等待first()
类型的getdatastoresAccess
操作,并将其映射到setCurrentDatastoreId
和setCurrentItemId
您的问题1)的答案是
setCurrentProjectId
被用作switchMap
的(忽略)输入,然后映射到setCurrentDatastoreId
和setCurrentItemId
谢谢您的回答和解释,我现在理解了问题1。我尝试了您的代码,但出现了错误(你可以在主要帖子的编辑中看到)。但是逻辑就是你描述的。我会努力找到解决办法。谢谢教授,我错过了setCurrentProjectId
的,它将动作
转换为可观察的
。我已经更新了我的代码。哦,对了,我也没有注意到,好的,它工作得很好,谢谢你
const goToItemEpic = (action$, state$): Observable<any> => action$.pipe(
ofType(ItemsDetailsActions.goToItem),
concatMap((action: {payload: {projectId: string; datastoreId: string; itemId: string}}) =>
concat(
of(ProjectActions.setCurrentProjectId({projectId: action.payload.projectId})),
action$.pipe(
ofType(ItemsDetailsActions.getDatastoresSuccess),
first(),
switchMap(() => of(
DatastoreActions.setCurrentDatastoreId({datastoreId: action.payload.datastoreId}),
ItemsActions.setCurrentItemId({ itemId: action.payload.itemId })
)
})