处理监听图表的函数,确保及时移除&生产环境不打印log

This commit is contained in:
2026-04-22 11:07:10 +08:00
parent 845e5a8af3
commit cfcb4f5068
117 changed files with 6781 additions and 5767 deletions

View File

@@ -38,7 +38,8 @@ export default {
'rgba(255, 123, 123, 1)', // 额外补充颜色,适配更多数据项
'rgba(153, 102, 255, 1)'
],
myChart: null // 保存图表实例,便于销毁和缩放
myChart: null, // 保存图表实例,便于销毁和缩放
resizeHandler: null // 窗口resize事件处理器
};
},
computed: {},
@@ -56,6 +57,25 @@ export default {
this.$nextTick(() => {
this.initData(); // 初始化图表
});
// 注册窗口resize事件使用稳定的引用以便后续移除
this.resizeHandler = () => {
if (this.myChart) {
this.myChart.resize();
}
};
window.addEventListener('resize', this.resizeHandler);
},
beforeDestroy() {
// 移除窗口resize事件监听器
if (this.resizeHandler) {
window.removeEventListener('resize', this.resizeHandler);
this.resizeHandler = null;
}
// 销毁图表,避免内存泄漏
if (this.myChart) {
this.myChart.dispose();
this.myChart = null;
}
},
methods: {
initData() {
@@ -229,18 +249,6 @@ export default {
// 设置图表配置
option && this.myChart.setOption(option);
// 窗口缩放监听(优化:避免重复绑定)
const resizeHandler = () => {
this.myChart && this.myChart.resize();
};
window.addEventListener('resize', resizeHandler);
// 组件销毁清理
this.$once('hook:destroyed', () => {
window.removeEventListener('resize', resizeHandler);
this.myChart && this.myChart.dispose();
});
}
},
};