我有以下服务来容纳我的应用中的全局微调器:
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;
然后在我的主应用程序组件中的router-outlet
上方:
<app-spinner *ngIf="spinnerService.getSpinnerVisibility() | async "></app-spinner>
问题是,async
此处管道是否正常运行以取消订阅而没有来自此ReplaySubject
的内存泄漏,或者我是否必须手动取消订阅?
答案 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
.