angular - 无法从 store 中选择最新的 ID,withLatestFrom 返回以前的 ID 而不是最后一个,为什么?
问题描述
我正在尝试创建一个主/详细页面,我需要主 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 之前添加 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()) 不能提供实际最新的下一个事件的非零值,但提供前一个的零值!
推荐阅读
- sql - 如何获取 SQL 中前 X 最常见的字段?
- c# - .Net Core API 从数据库中计算项目
- mysql - 如何使用 Qt 检查 mysql 数据库是否存在?
- ios - 从 Firebase / Firestore 查询中读取数组的内容
- python - 迁移学习可训练模型在保存时抛出错误
- php - 使用事件日历和 Ajax 加载更多内容来拉下一个事件
- css - 如何有效地打破 vuetify 组件内的内容行?
- c++ - 有没有办法可以控制输入以阻止用户输入空格?
- javascript - 在 JS (Node.js) 中同时管理多个长时间运行的任务
- java - 在 android studio 中使用查询从 firebase 检索数据