首页 > 解决方案 > 将 WithLatestFrom 中的值传递给另一个运算符 - Rxjs,Ngrx 效果

问题描述

我有这个 ngrx 效果,我想使用我所在州的数据withLatestFrom

$loadProduct = createEffect(() =>
  this.actions$.pipe(
    ofType(fromActions.loadProduct),
    withLatestFrom(this.store.select(fromSelectors.debounceTime)),
    delay(debounceTime), // how can I get the debounceTime value up here?
    map(_ => fromActions.foo())
  )
)

如何在延迟范围内获得这个 debounceTime 值?

标签: rxjsngrx

解决方案


由于delayis just delayWhen(() => timer(duration));,我认为您可以使用它delayWhen来解决问题:

$loadProduct = createEffect(() =>
  this.actions$.pipe(
    ofType(fromActions.loadProduct),
    withLatestFrom(this.store.select(fromSelectors.debounceTime)),
    
    delayWhen(([, debounceTimeVal]) => timer(debounceTimeVal)),

    map(_ => fromActions.foo())
  )
)

作为旁注,delayWhen这只是一个有趣的用例mergeMap:而不是发出内部值,而是发出外部值。


推荐阅读