From 376591e2ada7cfa4b7b461b3d5a97aea4803ee58 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sun, 24 Jul 2022 10:20:04 +0200 Subject: [PATCH] add: handle subscriptions in app component --- src/app/app.component.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index e4c1ead..08e5680 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,6 +1,10 @@ -import { Component, ViewChild, AfterViewChecked } from '@angular/core'; +import { Component, + ViewChild, + AfterViewChecked, + OnDestroy } from '@angular/core'; import { SafeUrl } from '@angular/platform-browser'; -import { Observable } from 'rxjs'; +import { Observable, + Subscription } from 'rxjs'; import { ITitle } from './ititle'; import { ISubtitle } from './isubtitle'; import { IFood } from './ifood'; @@ -15,8 +19,10 @@ import { version } from '../environments/version'; styleUrls: ['./app.component.css'] }) -export class AppComponent implements AfterViewChecked +export class AppComponent implements AfterViewChecked, OnDestroy { + private subscriptions: Subscription[] = []; + @ViewChild("exportJsonLink") exportJsonLink: any; @ViewChild("exportHtmlLink") exportHtmlLink: any; public foodcard: IFoodCard = { Titles: [] }; @@ -31,25 +37,25 @@ export class AppComponent implements AfterViewChecked public exportJson(): void { let observ: Observable = this.jsonFileService.exportJson(this.foodcard); - observ.subscribe((next) => { + this.subscriptions.push(observ.subscribe((next) => { this.downloadJsonHref = next; - }); + })); } public exportHtml(): void { let observ: Observable = this.htmlExportService.exportHtml(this.foodcard); - observ.subscribe((next) => { + this.subscriptions.push(observ.subscribe((next) => { this.downloadHtmlHref = next; - }); + })); } public fileEvent(event: any): void { let importobserv: Observable = this.jsonFileService.importJson(event); - importobserv.subscribe((next) => { + this.subscriptions.push(importobserv.subscribe((next) => { this.foodcard = next; - }); + })); } public addTitle(): void @@ -71,4 +77,9 @@ export class AppComponent implements AfterViewChecked Promise.resolve().then(() => this.downloadHtmlHref = ""); } } + + public ngOnDestroy(): void + { + this.subscriptions.forEach(s => s.unsubscribe()); + } } -- 2.39.5