代码之家  ›  专栏  ›  技术社区  ›  Michahell

仅从存储中选择最近的值

  •  1
  • Michahell  · 技术社区  · 6 年前

    一些这样的搜索让我相信没有好的(简单的)方法来实现标题中的问题。以下线程都非常相关:

    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)-根据 如何用@ngrx/store获取状态对象的当前值? )因为它已经被移除了。

    1 回复  |  直到 6 年前
        1
  •  3
  •   Michahell    6 年前

    我忘了补充一点,我的印象是 store.select(...).pipe(first()) 不返回存储区中的最新项。但它似乎是这样的,它返回的任何错误值的原因都在于我们自己的代码。

    store.select(…).pipe(first()) 确实是这样:从 Store (并完成 Observable ).