在我的 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 运算符可以给我这种行为?
谢谢
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)
)