簡體   English   中英

在導航到另一個組件之前銷毀 ng2-charts

[英]Destroy ng2-charts before navigating to another component

我在每條路線中創建了圖表。

當我嘗試從一個組件導航到另一個組件時,出現此錯誤並且頁面已損壞。 (請看下面的截圖)

任何解決方案?

謝謝

在此處輸入圖像描述

 <canvas baseChart id="barChart" #barChart [datasets]="chartData" [labels]="barChartLabels" [options]="barChartOptions" [chartType]="chartType"></canvas>

在銷毀之前檢查圖表是否存在:
應用程序組件.ts

import {BaseChartDirective} from 'ng2-charts';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent implements OnDestroy {
  @ViewChild('barChart') chart: BaseChartDirective;

  ngOnDestroy() {
    if (this.chart) this.chart.ngOnDestroy();
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM