У меня есть компонент отчета, который имеет подписку Behavior Subject, и он извлекает данные из веб-интерфейса после .next вызова.Когда я перемещаюсь на другую страницу за страницей отчета, компонент отчета все еще жив и продолжает отправлять вызовы в webApi.
Как уничтожить компоненты после перехода к другой или отписаться от темы поведения.ПодробнееУ меня есть несколько компонентов отчета, он отображает отчеты разных типов.Один из компонентов ниже
@destroyAllSubscribers()
export class StandardReportComponent implements OnInit {
public subscriptions: Subscription[] = [];
reportData = [];
showReport=false;
constructor(private reportService: ReportService)
{
this.subscriptions.push(this.reportService.chartType.subscribe(chartType => {
if (this.currentChart != ReportChartType.None){
this.showReport=false; //Used in *ngIf to show report HTML template
this.reportService.getReportData().subscribe(result=>{
this.reportData=result;
this.showReport=true; //Used in *ngIf to show report HTML template
}
}
}
}
У меня есть уничтожитель-декоратор подписчика, который запускается при уничтожении компонента,
Код:
export function destroyAllSubscribers() {
return (targetComponent: any) => {
targetComponent.prototype.ngOnDestroy = ngOnDestroyDecorator();
function ngOnDestroyDecorator() {
return function () {
if (this.subscriptions != undefined)
{
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
};
});
}
};
}
};
}
Следует отписаться;однако вся подписка выполняется после перехода на другую страницу.