refactor(demo): unsubscribe when destroy component

This commit is contained in:
KostyaDanovsky 2017-09-20 14:31:06 +03:00
parent 549be765f0
commit cceabef8eb
12 changed files with 96 additions and 37 deletions

View file

@ -1,4 +1,4 @@
import { AfterViewInit, Component } from '@angular/core';
import { AfterViewInit, Component, OnDestroy } from '@angular/core';
import { NbThemeService } from '@nebular/theme';
declare const echarts: any;
@ -10,11 +10,11 @@ declare const echarts: any;
<div echarts [options]="option" class="echart"></div>
`,
})
export class ElectricityChartComponent implements AfterViewInit {
export class ElectricityChartComponent implements AfterViewInit, OnDestroy {
option: any;
data: Array<any>;
themeSubscription: any;
constructor(private theme: NbThemeService) {
@ -41,7 +41,7 @@ export class ElectricityChartComponent implements AfterViewInit {
}
ngAfterViewInit(): void {
this.theme.getJsTheme().delay(1).subscribe(config => {
this.themeSubscription = this.theme.getJsTheme().delay(1).subscribe(config => {
const eTheme: any = config.variables.electricity;
this.option = {
@ -184,4 +184,8 @@ export class ElectricityChartComponent implements AfterViewInit {
};
});
}
ngOnDestroy() {
this.themeSubscription.unsubscribe();
}
}

View file

@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { Component, OnDestroy } from '@angular/core';
import { NbThemeService } from '@nebular/theme';
import { ElectricityService } from '../../../@core/data/electricity.service';
@ -8,7 +8,7 @@ import { ElectricityService } from '../../../@core/data/electricity.service';
styleUrls: ['./electricity.component.scss'],
templateUrl: './electricity.component.html',
})
export class ElectricityComponent {
export class ElectricityComponent implements OnDestroy {
data: Array<any>;
@ -16,12 +16,17 @@ export class ElectricityComponent {
types = ['week', 'month', 'year'];
currentTheme: string;
themeSubscription: any;
constructor(private eService: ElectricityService, private themeService: NbThemeService) {
this.data = eService.getData();
this.themeService.getJsTheme().subscribe(theme => {
this.themeSubscription = this.themeService.getJsTheme().subscribe(theme => {
this.currentTheme = theme.name;
});
}
ngOnDestroy() {
this.themeSubscription.unsubscribe();
}
}