redux 将WithlatestFrom用于多个相互依赖的可观察对象

oiopk7p5  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(104)

我正在尝试使用NgRx 12创建Angular 效果。我需要使用多个选择器的数据,但其中一个选择器(isLoading)的参数来自第一个选择器。是否可以从此处获取数据?

withLatestFrom(
        this.store.pipe(select(fromA.getFormData)), // need to get data from here
        this.store.pipe(select(fromA.getFiles)),
        this.store.pipe(select(fromB.isLoading(formData.someValue // to here)),
        (action: Action, formData: any, files: Array<Attachment>) => ({ formData, files, isLoading })
      ),
      mergeMap(({ formData, files, isLoading }) => {
      ...

谢谢以前!

nukf8bse

nukf8bse1#

也许有一个更好的方法,如果你也想在mergeMap中使用表单数据,你可以这样做

withLatestFrom(
 this.store.pipe(select(fromA.getFormData)).pipe(
  switchMap((theFormData) => this.store.select(fromB.isLoading(theFormData)))
 ),
 this.store.select(fromA.getFiles),
 this.store.pipe(select(fromA.getFormData))
),
mergeMap([theLoadingValue, theFiles, theFormValue] => {...})

如果您不关心表单数据,那么它将简单地是:

withLatestFrom(
 this.store.pipe(select(fromA.getFormData)).pipe(
  switchMap((theFormData) => this.store.select(fromB.isLoading(theFormData)))
 ),
 this.store.select(fromA.getFiles)
),
mergeMap([theLoadingValue, theFiles] => {...})

相关问题