【问题标题】:Can not select the latest ID from store, withLatestFrom returns previous ID and not last, why?无法从 store 中选择最新的 ID,withLatestFrom 返回以前的 ID 而不是最后一个,为什么?
【发布时间】:2021-01-12 09:10:29
【问题描述】:

我正在尝试创建一个主/从页面,我需要主 ID 来检索详细列表的相关记录。

我有一个选择器

export const selectRecords= (state: CoreState) => state.records;

export const getId = createSelector(selectRecords,
    (state: myState) => state && state.selectedRecords && state.selectedRecords.id
);

实际上,我正在尝试从商店获取最新的 ID,例如:

@Effect()
    getSomething$ = this.actions$.pipe(
        ofType<GetMyRecords>(MyType.GET_RECORDS),
        withLatestFrom(this.store.select(getId)),
        switchMap(([action, id]) => {
            this.log.debug('Get ID', id);
            ... calling my service here
        })
    );

但即使我使用withLatestFrom,我得到的是以前的 ID,而不是最新的。

当我从 Redux 检查控制台时,状态中的 selectedRecords.id 是正确的(最新的),是我想要检索的。

我知道这是一个很长的问题,但是有没有人有提示或建议我可以尝试从商店获取正确的最后一个 ID?

【问题讨论】:

  • 你能不能把withLatestFrom(this.store.select(getId))换成switchMap((action) =&gt; this.store.select(getId).pipe(map(id =&gt; ([action, id])), take(1)))看看有没有帮助,
  • @WillTaylor 结果相同。我第一次遇到错误,然后当我回到我的列表并从我的列表到我必须根据主服务器的 ID 加载网格的详细信息时,它使用的是以前访问的详细信息 id。就好像它总是落后一步,即使在 REDUX 中我看到了正确的 ID。
  • 也许它有帮助,但我只是尝试了 combineLatest 而不是 withLatestFrom 并且它有效。但我收到警告“combineLatest”已弃用。
  • 我试图替换它并从“rxjs”而不是“rxjs/operators”的 combineLatest 加载它,但像这样它需要第二个参数,而我所做的并不是真正结合两个选择。我不知道与运营商合作过...

标签: angular rxjs ngrx typescript-typings


【解决方案1】:

我设法通过在 withLatestFrom 之前添加 debounceTime(0) 来解决问题:

更新工作代码为:

@Effect()
    getSomething$ = this.actions$.pipe(
        ofType<GetMyRecords>(MyType.GET_RECORDS),
        debounceTime(0),
        withLatestFrom(this.store.select(getId)),
        switchMap(([action, id]) => {
            this.log.debug('Get ID', id);
            ... calling my service here
        })
    );

我正在添加一个链接来描述我遇到的确切问题: https://github.com/ReactiveX/RxSwift/issues/1121

换句话说 .withLatestFrom(item.asObservable()) 不能提供 实际最新的下一个事件的非零值,但提供上一个 一个零值!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2011-03-09
    • 2012-09-12
    • 2021-05-24
    • 1970-01-01
    • 2021-07-09
    相关资源
    最近更新 更多