有没有遇到过这样一个问题:在本地打开弹框的图表时,能正常打开,发布到线上时,打开过一次,第二次打开就会出现白屏现象。
原因:
echarts 频繁初始化画图时候有内存泄漏,吃掉了所有内存,就白屏画不出来了。
生成的echarts实例对象很大,占用内存过多。 echarts 里 zrender 用的canvas。
解决办法:
1.把创建的echarts变成全局遍历
2.在初始化echarts时判断
3.关闭时调用echarts.dispose(xxChart);
<template>
<div style="width: 100%;">
<el-dialog draggable v-model="dialogVisible" title="业绩统计" width="70%" style="min-height: 500px" :before-close="close">
<div style="width: 100%;height: 600px; display: inline-block;" ref="orderMain"></div>
<template #footer>
<span class="dialog-footer">
<el-button @click="close">取消</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
const orderMain = ref(null)
var orderChart = null
function initChar() {
// 初始化dom前调用,判断是否已存在echarts初始化的dom
orderChart = echarts.getInstanceByDom(orderMain.value)
if (orderChart == null) {
orderChart = echarts.init(orderMain.value);
}
}
/** 关闭弹框前调用 **/
function close() {
if (orderChart != null) {
echarts.dispose(orderChart);
}
dialogVisible.value = false
}
参考:https://www.cnblogs.com/gaoyd/p/13613877.html