RxJS管道Finalize运算符未被调用

问题描述 投票:0回答:1
import {
    Observable,
    BehaviorSubject
} from 'rxjs';
import {
    finalize,
    share
} from 'rxjs/operators'

export class someComponent() {

    public count$ = new BehaviorSubject < any > (0);

    public constructor() {
        this.shareResponse()
            .pipe(
                finalize(() => {
                    console.log('finalize called');
                }))
            .subscribe((event: any) => {
                // Do something
            });
    }
    public shareResponse(): Observable < any > {
        return this.count$.pipe(share());
    }
    public countChanged(event) {
        this.count$.next(event);
    }
}

HTML:

    <some-tag(countChanged) = (countChanged($event)) > < /some-tag>
rxjs angular6 angular7
1个回答
0
投票

BehaviorSubject不会完成,除非您通过致电自己完成this.count$.complete()。这就是为什么finalize()不会发生的原因,因为它正在等待Observable完成。

看一下StackBlitz上的代码示例,请参见link

© www.soinside.com 2019 - 2024. All rights reserved.