有关使用从商店中选择的状态的效果的 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/