RxJS 管道 Finalize 操作符没有被调用

su1*_*212 2 rxjs angular6 angular7

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

HTML:

    <some-tag(countChanged) = (countChanged($event)) > < /some-tag>
Run Code Online (Sandbox Code Playgroud)

Gog*_*eli 6

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

查看 StackBlitz 上的代码示例,请参阅链接