echarts不会根据页面窗口变化而调整图表的大小,在当前页面的 script 中加入这一段可辅助当前页面刷新
data: {
return {
screenWidth: document.body.clientWidth, // 屏幕宽度
timer: false
}
},
mounted () {
// 监听窗口大小
window.onresize = () => {
return (() => {
this.screenWidth = document.body.clientWidth
})()
}
},
watch: {
screenWidth (val) {
// 为了避免频繁触发resize函数导致页面卡顿,使用定时器
if (!this.timer) {
// 一旦监听到的screenWidth值改变,就将其重新赋给data里的screenWidth
this.screenWidth = val
this.timer = true
let that = this
setTimeout(function () {
that.getECharts() // 操作
that.timer = false
}, 400)
}
}
},
methods: {
getECharts () {
var myChart = echarts.init(chartDom);
......
myChart.resize() // 刷新
}
}
本文介绍了一种方法,通过监听窗口resize事件并设置防抖定时器,确保Echarts图表能根据浏览器窗口尺寸变化自动调整大小,从而避免页面卡顿问题。在mounted生命周期钩子中绑定了resize事件处理函数,并在watch中对screenWidth进行监听,当尺寸变化时,通过延迟调用resize方法更新图表。

5万+

被折叠的 条评论
为什么被折叠?



