管道操作符时如何返回可观察的

管道操作符时如何返回可观察的

本文介绍了管道操作符时如何返回可观察的`forkJoin`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我拥有这个运行良好的解析器之前:

Before I had this resolver that just worked fine:

resolve() {
    return forkJoin(
        this.getData1(),
        this.getData2(),
        this.getData3()
    );
}

现在我必须做一些实际上不起作用的事情:

Now I have to do something like that which is actually does not work:

  resolve() {
    return this.actions$
      .pipe(
        ofActionSuccessful(SomeSctonSuccess),
        forkJoin(
           this.getData1(),
           this.getData2(),
           this.getData3()
        )
      );
    }

因为我遇到了这个错误:

as I am hitting this error:

'Observable' 类型的参数不可赋值到OperatorFunction"类型的参数.类型'Observable' 不提供签名的匹配项'(来源:Observable):Observable'.

任何想法如何解决?

现在我只在 ofActionSuccessful(SomeSctonSuccess) 发生后才注意返回我的 forkJoin https://ngxs.gitbook.io/ngxs/advanced/action-handlers

Now I heed to return my forkJoin only after ofActionSuccessful(SomeSctonSuccess) is taking place https://ngxs.gitbook.io/ngxs/advanced/action-handlers

推荐答案

使用 exhaustMap 操作符.它映射到内部 observable,忽略其他值直到该 observable 完成

Use exhaustMap operator. It maps to inner observable, ignore other values until that observable completes

import { forkJoin } from 'rxjs';
import { exhaustMap } from 'rxjs/operators';

resolve() {
    return this.actions$
      .pipe(
        ofActionSuccessful(SomeSctonSuccess),
        exhaustMap(() => {
         return forkJoin(
             this.getData1(),
             this.getData2(),
             this.getData3()
           )
       })

      );
    }

这篇关于管道操作符时如何返回可观察的`forkJoin`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:08
查看更多