【发布时间】:2023-03-28 19:50:01
【问题描述】:
编辑我有以下 =>
@Effect()
createMission$ = this.actions$.pipe(
ofType<featureActions.CreateMissionRequest>(featureActions.ActionTypes.CreateMissionRequest),
switchMap((action) =>
this.store$.pipe(select(MissionsStoreSelectors.getById(), {id : action.payload.routeId}))),
switchMap((mission) => this.dataService.createMission(this.APIMissionFromMissionRoute(mission)).pipe(
map(response => new featureActions.CreateMissionSuccess({response, mission})),
catchError((error: HttpErrorResponse) => {
return of(new featureActions.CreateMissionFailed({error}));
}),
)
)
);
目前没有服务器,但我用 observable 伪造了回复
createMission(params: API_MODEL.IMissionAPI): Observable<API_MODEL.CreateMissionResponse> {
return new Observable(observer => {
setTimeout(() => {
observer.next({
result: {
status: 2
},
missionID: {
identifier: 41
}
});
observer.complete();
}, 120);
});
}
问题是,我的第二个 switchmap
switchMap((mission) => this.dataService.createMission(this.APIMissionFromMissionRoute(mission))
正在循环。这是因为我的第一个 switchMap 正在获取一个带有参数的选择器,当我的状态更新时,它会再次触发我的第二个 switchmap。
我想知道是否有办法以另一种方式获取我的当前状态。
我尝试了withLatest,但是我需要访问当前的action.payload才能得到我想要的,我无法将action传递给withLatest
【问题讨论】: