diff --git a/components/icon/icon.service.ts b/components/icon/icon.service.ts index 431c7c13cb..1b80b87caf 100644 --- a/components/icon/icon.service.ts +++ b/components/icon/icon.service.ts @@ -5,9 +5,9 @@ import { DOCUMENT } from '@angular/common'; import { HttpBackend } from '@angular/common/http'; -import { Inject, Injectable, InjectionToken, Optional, RendererFactory2, Self } from '@angular/core'; +import { Inject, Injectable, InjectionToken, OnDestroy, Optional, RendererFactory2, Self } from '@angular/core'; import { DomSanitizer } from '@angular/platform-browser'; -import { Subject } from 'rxjs'; +import { Subject, Subscription } from 'rxjs'; import { IconDefinition, IconService } from '@ant-design/icons-angular'; @@ -31,10 +31,18 @@ export const DEFAULT_TWOTONE_COLOR = '#1890ff'; @Injectable({ providedIn: 'root' }) -export class NzIconService extends IconService { +export class NzIconService extends IconService implements OnDestroy { configUpdated$ = new Subject(); private iconfontCache = new Set(); + private subscription: Subscription | null = null; + + ngOnDestroy(): void { + if (this.subscription) { + this.subscription.unsubscribe(); + this.subscription = null; + } + } normalizeSvgElement(svg: SVGElement): void { if (!svg.getAttribute('viewBox')) { @@ -81,7 +89,7 @@ export class NzIconService extends IconService { } private onConfigChange(): void { - this.nzConfigService.getConfigChangeEventForComponent('icon').subscribe(() => { + this.subscription = this.nzConfigService.getConfigChangeEventForComponent('icon').subscribe(() => { this.configDefaultTwotoneColor(); this.configDefaultTheme(); this.configUpdated$.next();