【问题标题】:Chaining RxJS map operators and ngrx effects Issue链接 RxJS 映射运算符和 ngrx 效果问题
【发布时间】:2017-07-22 20:45:07
【问题描述】:

我的应用使用了 ngrx 和 ngrx 效果。这是我的应用效果之一:

  @Effect()
  reloadPersonalInfo$: Observable<Action> = this.actions$
    .ofType(currentUserAccount.ActionTypes.RELOAD_PERSONAL_INFO)
    .filter(() => <boolean>JSON.parse(localStorage.getItem('authenticated')))
    .switchMap(() =>
      this.userAccountService
        .retrieveCurrentUserAccount()
        .map(currentUserAccount => new LoadUserAccountAction(currentUserAccount))
        .map(() => new SigninAction())
    );

我想知道为什么 LoadUserAccountAction 不进入我的减速器函数,除非我注释掉 //.map(() =&gt; new SigninAction())

有人可以帮忙吗?我做错了什么?

【问题讨论】:

  • 效果只返回一个动作。
  • @Jim:感谢您的评论和编辑。您能否建议一种方法来更改应用程序的设计,以避免从效果中返回多个操作?
  • 您正试图同时返回“LoadUserAccountAction”和“SigninAction”。您应该退回其中之一。换句话说,删除代码中的 .map 行之一。
  • 我指的是carant的回复。你对只返回一个动作的评论是否也适用于返回几个与concatMap“连接”的动作(见下面的回复)?

标签: rxjs rxjs5 ngrx ngrx-effects


【解决方案1】:

您的LoadUserAccountAction 没有被分派,因为它不是由效果发出的,因为最终的.map(() =&gt; new SigninAction()) 看到的是SigninAction 发出的。

可以从一个效果中发出多个动作,你只需要这样做:

@Effect()
reloadPersonalInfo$: Observable<Action> = this.actions$
  .ofType(currentUserAccount.ActionTypes.RELOAD_PERSONAL_INFO)
  .filter(() => <boolean>JSON.parse(localStorage.getItem('authenticated')))
  .switchMap(() => this.userAccountService
    .retrieveCurrentUserAccount()
    .concatMap(currentUserAccount => [
      new LoadUserAccountAction(currentUserAccount),
      new SigninAction()
    ])
  );

concatMap 运算符将展平包含这两个动作的数组,以便发出这两个动作 - 按照它们在数组中声明的顺序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 2014-10-24
    • 2023-04-08
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多