异步管道取消订阅ReplaySubject

时间:2017-05-16 09:13:48

标签: angular spinner rxjs

我有以下服务来容纳我的应用中的全局微调器:



import {
  Injectable
} from '@angular/core';
import {
  Observable
} from 'rxjs/Observable';
import {
  ReplaySubject
} from 'rxjs/ReplaySubject';

@Injectable()
export class SpinnerService {
  private visible = new ReplaySubject < boolean > ();

  showSpinner() {
    this.visible.next(true);
  }

  hideSpinner() {
    this.visible.next(false);
  }

  getSpinnerVisibility(): Observable < boolean > {
    return this.visible.asObservable();
  }
}
&#13;
&#13;
&#13;

然后在我的主应用程序组件中的router-outlet上方:

<app-spinner *ngIf="spinnerService.getSpinnerVisibility() | async "></app-spinner>

问题是,async此处管道是否正常运行以取消订阅而没有来自此ReplaySubject的内存泄漏,或者我是否必须手动取消订阅?

1 个答案:

答案 0 :(得分:2)

There is absolutely no difference between a ReplaySubject and an Observable from the subscriber's point of view. You don't have to unsubscribe from an observable when you use the async pipe, so it's the same for a ReplaySubject.