rxjs - 需要帮助理解这个 Ngrx 效果

标签 rxjs ngrx ngrx-store ngrx-effects

有关使用从商店中选择的状态的效果的 Ngrx 文档 状态(没有双关语意)

Note: For performance reasons, use a flattening operator like concatMap to prevent the
selector from firing until the correct action is dispatched.

...并提供示例:

    addBookToCollectionSuccess$ = createEffect(() => this.actions$.pipe(
      ofType(CollectionApiActions.addBookSuccess),
      concatMap(action => of(action).pipe(
          withLatestFrom(this.store.select(fromBooks.getCollectionBookIds))
      )),
      tap(([action, bookCollection]) => console.log('whatever'))
      ), { dispatch: false }
    );

此示例已更改为使用 concatLatestFrom,但这在这里并不重要。我感兴趣的是理解最初的建议。我想我理解的意图是避免 withLatestFrom 处理来自 getCollectionBookIds 的结果,除了源操作触发时,但我不明白为什么解决方案需要涉及包装withLatestFrom 在使用 of(action) 手动构造的 Observable 中。这样的东西不会同样有效并且更具可读性吗?

    concatMap(() => this.store.select(selectSelectedBookIds).pipe(first())),

我对 Rxjs 的理解不是很好,在尝试解决这个问题几天后,我怀疑我仍然遗漏了一些东西。

最佳答案

如果您只需要选定状态,您的建议应该可以正常工作:

concatMap(() => this.store.select(selectSelectedBookIds).pipe(first())),

但是,如果您还需要操作本身,以下将传递一个包含操作和您选择的状态的数组:

concatMap(action => of(action).pipe(
  withLatestFrom(this.store.select(fromBooks.getCollectionBookIds))
)),

关于rxjs - 需要帮助理解这个 Ngrx 效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66250173/

相关文章:

angular - 如何将 NgRx 8 操作与 NgRx 7 reducer 一起使用

angular - @ngrx/effects 给出 TypeError : You provided 'undefined' where a stream was expected. 您可以提供 Observable、Promise、Array 或 Iterable

angular - ngrx Reducer 改变多个状态

rxjs。在订阅方法中添加代码是一个好的做法吗?

Angular - 如何最好地处理服务及其订阅中的主题?

angular - ngrx 仅在存储为空时从服务器加载数据

javascript - 如何通过运算符链将多个值传递给映射运算符?在 NgRx 效果中

angular - 空注入(inject)器错误 : No provider for ReducerManager

angular - rxjs pairwise 发出重复值

javascript - angular ngrx store 参数化内存选择器