Async pipe to unsubscribe from ReplaySubject

纵饮孤独 提交于 2019-12-11 08:37:04

问题


I have the following service to accommodate for a global spinner in my app:

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();
  }
}

Then the following just above my router-outlet in my main app component:

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

The question is, should the async pipe here function as normal to unsubscribe without memory leaks from this ReplaySubject or do I have to manually unsubscribe?


回答1:


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.



来源:https://stackoverflow.com/questions/43997154/async-pipe-to-unsubscribe-from-replaysubject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!