我收到错误 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,
},
});
添加以下代码代替 if 条件
var exists = Chart.getChart('MyChart');
exists?.destroy();