combineLatest 的 RXJS 运算符,不带空值

问题描述 投票:0回答:1

在我的 Angular 项目中,我试图获取一些 ngrx 选择器的值,并使用它们全部创建一个用于 API 调用的查询字符串。

我想等到它们都发出非空值,然后只发出一次(因此,如果值随时间变化,则不会多次发送 api 调用)。 这是我得到的解决方案:

const selectorA$ = this.store.select(...)
const selectorB$ = this.store.select(...)
const selectorC$ = this.store.select(...)

return combineLates([selectorA$, selectorB$, selectorC$]).pipe(
   first(list => list.every(item => item != null)),
   map(([selectorA, selectorB, selectorC]) => //... return query string using the store values)
)

我想知道这是否是正确的方法,或者是否有内置的 rxjs 运算符可以给我这种行为?

谢谢

angular rxjs observable ngrx ngrx-store
1个回答
0
投票

您可以将

combineLatest
filter
一起使用,以允许流仅在所有三个值都存在时继续进行!

const selectorA$ = this.store.select(...)
const selectorB$ = this.store.select(...)
const selectorC$ = this.store.select(...)

return combineLatest([selectorA$, selectorB$, selectorC$]).pipe(
   filter(list => list.every(item => item != null)),
   map(([selectorA, selectorB, selectorC]) => //... return query string using the store values)
)
© www.soinside.com 2019 - 2024. All rights reserved.