订阅Combinebest中的所有可观察物

时间:2019-09-23 08:31:48

标签: angular typescript rxjs subscription combinelatest

在我的代码中,打开一个matDialog,允许XLSX提取。

打开此matDialog时,数据会加载到ngOnInit()中,这可能需要一些时间。 这就是为什么我将combineLatest()filter()一起使用的原因,使我可以等待三个查询的结果以允许提取。 我现在想添加一个mat-progress-bar determinate来查看提取的去向。 为此,我希望能够根据收到的每个结果来修改我的变量progress

因此,我在每个请求上都使用了map(result => {this.progress = this.progress + 30;}以增加进度。 它有效,但是破坏了我的combineLatest()。进度停止在90%,我找不到解决方案。

代码:

this.subscriptions$.add(combineLatest([
    this.storeWallets.loadInvoiceExtractionProduct(this.selectedWallet).pipe(
        //map(result => { this.progress = this.progress + 30; }),
        catchError(err => {
            console.error(err.message);
            return of(err);
        })
    ),
    this.storeWallets.loadInvoiceExtractionSupport(this.selectedWallet).pipe(
        //map(result => { this.progress = this.progress + 30; }),
        catchError(err => {
            console.error(err.message);
            return of(err);
        })
    ),
    this.storeWallets.loadInvoiceExtractionTraining(this.selectedWallet).pipe(
        //map(result => { this.progress = this.progress + 30; }),
        catchError(err => {
            console.error(err.message);
            return of(err);
        })
    ),
    ]).pipe(
        filter(results => results.every(res => !!res))
    ).subscribe(results => {
        //this.progress = 100;
        document.body.style.cursor = "default";
        this.waitExtraction = false;
    })
);

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

此行:

map(result => { this.progress = this.progress + 30; }),

将获取所有结果并返回 undefined ,因为没有return语句(因此filter将过滤出结果,并且订阅服务器将不会运行)。将map替换为tap运算符,它应该可以工作。