无法在 rxjs 中有效分派两个动作

Unable to dispatch two actions within effect in rxjs

我正在分派两个动作,不需要按顺序执行。我尝试了以下代码:

 @Effect()
   loadIntResponse$ = 
   this.actions.ofType(fromActions.LOAD_INT_RESPONSE).pipe(
   switchMap(() => {
     return this.Service.int().pipe(
    tap(x => this.store.dispatch(new fromActions.LoadService(x))),
    map(responseResult => {
     return new fromActions.LoadIntResponseSuccess(responseResult)
    }),
    catchError(error => {
      let err: responseResult = {
        status: "FALSE",
        message: error
      }
      return of(new fromActions.LoadIntResponseFail(err))
    })
  )
 })
)

loadService$ = this.actions.ofType(fromActions.Load_Service).pipe(
    switchMap((action: fromActions.LoadService) => {
    if (action.payload.status == 'TRUE' {
    return this.reuseService.reuseServiceCall().pipe(
    map( result ==> {
        return of({type: fromActions.LOAD_SERVICE_SUCCESS, payload: 
        result})
   }),
   catchError(error => {
      let err: responseResult = {
        status: "FALSE",
        message: error
      }
      return of(new fromActions.LoadServiceFail(err))
    })
  )
 })
)

但是没有触发另一个效果LoadService()

预期结果:触发另一个效果LoadService()

LoadService 接受任何类型的参数。

非常感谢您的帮助。谢谢!

我做了一些修改以符合您在评论中所说的内容。首先,你有一个调用 int() 的效果,如果成功,将发送一个 LoadIntResponseSuccess,但如果不成功,将转到 catchError。你有第二个效果,它会监听 LoadIntResponseSuccess 并会在 LoadIntResponseSuccess 被触发时立即发送 LoadService

第二个效果我可能是错的,因为我不知道你的操作传递的数据格式是怎样的,所以 x 可能是 x.something

loadService 效果现在首先过滤负载。你不能在 switchMap 中有一个允许你 return 某事或什么都没有的 if,如果你想要一个 if,你必须有一个 else!过滤后,调用服务,如果成功,则调度 LoadServiceSuccess 如果不成功,则转到 catchError.

 @Effect()
   loadIntResponse$ = 
   this.actions.ofType(fromActions.LOAD_INT_RESPONSE).pipe(
   switchMap(() => this.Service.int()),
   map((responseResult) => new fromActions.LoadIntResponseSuccess(responseResult)),
   catchError((error: any, effects: Observable<Action>) => {
      let err: responseResult = {
        status: "FALSE",
        message: error
      }
      return effect.pipe(startWith(new fromActions.LoadIntResponseFail(err)))
    })
  )

 @Effect()
   loadIntResponseSuccess$ = 
   this.actions.ofType(fromActions.LOAD_INT_RESPONSE_SUCCESS).pipe(
   map((action) => new fromActions.LoadService(action.payload))
  )



 @Effect()
    loadService$ = this.actions.ofType(fromActions.Load_Service).pipe(
       switchMap((action) => { if (action.payload.status === 'TRUE') {
         return [new fromActions.LoadServiceSuccess(action)]
        } else {
         return [new fromActions.LoadServiceFail()]
        } ),
         catchError((error: any, effects: Observable<Action>) => {
           let err: responseResult = {
             status: "FALSE",
             message: error
           }
           return effect.pipe(startWith(new 
             fromActions.LoadServiceFail(err)))
         })
       )