即使在离开页面并返回添加数据时使用 destroy() 后,Canvas 仍已在使用中

问题描述 投票:0回答:1

我收到错误 Canvas 已在使用中。必须先销毁 ID 为“ ”的图表,然后才能重新使用 ID 为“MyChart”的画布。对于控制台中的每个特定 id,该错误出现 4/5 次。我已经添加了 . 如果我留在页面中添加数据,不会显示错误,但如果我转到另一个页面并返回此组件添加数据,则会出现错误。

if (this.chart){
        this.chart.destroy(); 
        }  
this.chart = new Chart('MyChart', {
    type: 'line',
     data: {
     labels: monthLabels,
     datasets: [
      {
        label: 'Weight',
        data: weightLabels,
        backgroundColor: 'blue',
      },
    ],
  },
  options: {
    aspectRatio: 2.5,        
  },
}); 
angular typescript canvas chart.js destroy
1个回答
0
投票

添加以下代码代替 if 条件

var exists = Chart.getChart('MyChart');
exists?.destroy();
© www.soinside.com 2019 - 2025. All rights reserved.