【发布时间】:2023-01-26 12:00:03
【问题描述】:
我正在 Angular 应用程序中处理以下 RxJs 流,但在重置值时遇到了麻烦。我的应用程序中有多个位置在此处的 combineLatest 调用中向这三个可观察对象发出值,例如当用户更改过滤器设置或通过输入字段更新页面时。此外,我还有一个延迟加载功能,当用户接近底部时,它会向前移动页面。
当 name 和 filter 更新时,我希望它只返回来自 getContent 的最新数据,但是当 page observable 有一个新值时,我希望它通过 @ 将以前的数据与当前数据结合起来987654326@接线员。我遇到的唯一问题是我似乎无法在扫描中找到执行此操作的最佳方法,因为那时它不知道 mergeMap 来自 name 和 @987654329 的当前值和先前值@.
getContent(name, page filter) {
return this.http
.get(
`${this.API_BASE}/${name}/${filter}/${page}`
)
流如下所示:
this.results$ = combineLatest(
this.dataService.getName(),
this.dataService.getPage(),
this.dataService.getFilter()
).pipe(
mergeMap(([name, page, filter]) => {
this.dataService.getContent(name, filter, page);
}),
scan(
(
acc,
curr
) => {
this.nextPage = curr[curr.length - 1].id;
if (acc.length && curr.length) {
return acc.concat(curr);
}
return acc;
},
[]
)
);
该模板只是一个 div,它会循环并使用 async 管道进行更新,如果可能的话我想保留它。有没有更好的方法在单个流中处理这个问题,或者有什么方法可以让它按照我需要的方式进行分解?
【问题讨论】:
-
我不清楚您正在构建的流应该通知哪个
type。在scan运算符之外,您似乎得到了一个流,该流通知由this.dataService.getContent返回的类型的对象数组,另一方面,您说如果name或filter发出,您只需要传递this.dataService.getContent的结果,所以不是数组而是该类型的单个对象。你能澄清一下吗? -
在所有情况下,我都希望
Record<string, string>[]回来,我只是不希望在某些东西发出时组合acc + curr值,即name或filter,但当page发出时我会这样做。
标签: angular rxjs rxjs-pipeable-operators