【问题标题】:Unable to dispatch two actions within effect in rxjs无法在 rxjs 中调度两个有效的动作
【发布时间】:2020-10-15 00:59:40
【问题描述】:

我正在调度两个动作,不需要顺序执行。我试过下面的代码:

 @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 接受任何类型的参数。

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

【问题讨论】:

  • 这个效果应该做什么?你的 catchError 中的返回很可能不起作用。
  • 哪个影响LoadService()?
  • 不,只是我正在查看的代码
  • 这个效果假设调用 service.int() 将返回状态和消息。如果成功,则 LoadIntResponseSuccess 带有来自 service.int() 的响应结果并触发另一个效果 LoadService(x)。 x 也是来自服务调用 this.Service.int() 的响应结果。否则,在 catch 调用失败操作中传递响应结果,状态为 FALSE 并在消息中显示错误消息。
  • 好吧,这不是它的工作原理!基本上你想在 fromActions.LoadIntResponseSuccess 发生时调度新的 fromActions.LoadService(x)?

标签: angular rxjs ngrx store


【解决方案1】:

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

我可能对第二种效果有误,因为我不知道您的操作如何传递数据的格式,所以x 可能是x.something

loadService 效果现在首先过滤有效负载。你不能在switchMap 中有一个 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)))
         })
       )

【讨论】:

  • 我根据您之前的查询更新了上述问题。如果你的答案适合它
  • @CodeVenture 我不确定你的 loadService 效果是否正常,但其余的应该没问题或非常接近,loadService 效果会达到,但我不能说其余的是否正确。
  • 我稍后会告诉你。我想我必须按照您的建议更正我的 loadService。
  • @CodeVenture 我添加了 loadService 效果修改,对象格式可能是错误的(有效负载)但我是认真做的,所以没有任何类型的检查很难。您还应该开始输入所有内容!
  • 好好调试一下效果看看哪里丢了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
相关资源
最近更新 更多