从商店中仅选择最新值

时间:2018-07-30 14:07:51

标签: rxjs ngrx

在SO上进行一些搜索使我相信,没有很好的(简单的)方法来解决标题中的问题。以下线程都非常相关:

Get current state in ngrx

Getting current state in ngrx

Get current ngrx state without subscribe

Using ngrx to obtain store's current state once

How to get current value of State object with @ngrx/store?

从最后一个线程^看来,实现此目标的唯一方法是使用withLatestFrom()combineLatest()

不可能是以下情况,这是确保仅收到一项的唯一方法,并且该项是最新的一项:

of('terribleLatestValueHack').pipe(
  withLatestFrom(this.store.select(itemSelector))
  ).subscribe((stringAndItem: [string, Item]) => {
    const [, item] = stringAndItem;
    // do something with item
});

考虑到从商店中选择一个最新商品

  • 是一个(非常)简单的用例
  • 似乎是要求很高的功能

我真的很想知道为什么现有的支持(显然是在NGRX v2中-根据How to get current value of State object with @ngrx/store?)已被删除。

1 个答案:

答案 0 :(得分:1)

我忘了补充一点,我给人的印象是store.select(...).pipe(first())没有返回商店中的最新商品。但似乎确实如此,返回任何错误值的原因都在我们自己的代码中。

store.select(...).pipe(first())确实做到了:从Store返回一个当前项目(并完成Observable)。

相关问题