问题
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