在另一个动作 ngrx 的成功完全响应时触发一个动作

Trigger a action upon success full response of another action ngrx

我创建了一个 ngrx effect 负责向后端发送 POST 请求。现在我想稍微改变一下实现。如果 post 请求成功,我想触发另一个负责更新我的 ngrx 存储的操作。但我不知道该怎么做。

我尝试使用切换地图和合并地图但没有用。

creatTour$ = createEffect(() =>
   this.actions$.pipe(
       ofType(TourAction.createTour),
        mergeMap((action) => {
            const payload = action.tour;
            const endpoint = environment.urls.tour.replace("{tourProviderId}", "1");
             const request = new ServiceRequest(endpoint, 'POST', payload, options);

             return this.requestHandlerService.invoke(request).pipe(
                map((sucessResponce) => {
                   if (sucessResponce.code === 200) {
                        TourListAction.updateTourList({ tour: [tour] }) // i want to trigger this action and
                                                                            // below action 
                     }
                   return TourAction.createTourSucess({ responce: sucessResponce.message })
                    }),
                    catchError(err => {
                        const errorMessage = JSON.stringify(err);
                        console.log(errorMessage);
                        return of(TourAction.createTourFail({ errorMessage }))
                    })
                )
            })
        )
    );

我也试过这个方法

return [TourAction.createTourSucess({ responce: sucessResponce.message }
        TourListAction.updateTourList({ tour: [tour] })]

它抛出这个错误

 Property 'type' is missing in type '(({ responce: string; } 
 & TypedAction<"[Tour] Create Tour Success">) | ({ tour: Tours[]; } & 
 TypedAction<"[Tour List] Tour List Update">))[]' but required in type 
 'Action'.

这是更好的方法吗this.i 看到有一个叫做 entity 的新东西,我应该将它用于此实现吗?

为什么不在 createTourSucess 操作上更新您的状态?

您还可以return多项操作:

.pipe(
  switchMap(() => 
    return [
    TourListAction.updateTourList({ tour: [tour] }) ,  
    TourAction.createTourSucess({ responce: sucessResponce.message })
    ]
  )
)

https://medium.com/@amcdnl/dispatching-multiple-actions-from-ngrx-effects-c1447ceb6b22

你可以return你的效果中的多个动作,它们都会被调度。

https://medium.com/@tanya/understanding-ngrx-effects-and-the-action-stream-1a74996a0c1c 对于您的代码:

return successResponse.code === 200 ? [
    createTourUpdateAction(tour), 
    TourAction.createTourSuccess
    ] : [TourAction.createTourSuccess]