背景:
vue项目中用到echarts图表,页面上有侧边栏,侧边栏收缩图表不能自适应,想通过监听内容部分的宽度让图表resize,试过window带的resize,只能监听浏览器窗口大小变化,为了监听某元素区域的变化而使echarts的尺寸重置
本次解决采用 element-resize-detector 可以完美的解决
思路:
因为收缩侧边栏的时候右侧的区域会自动适应,但是echarts不会随之改变
element提供的 element-resize-detector 可以轻松解决问题的存在
转载参考:https://blog.csdn.net/csl125/article/details/89245267
vue文件
<template>
<div id="chart" style="width: 100%;height: 500px;"></div>
</template>
<script>
import echarts from 'echarts'
import resize from './mixins/index.js'
import {
debounce
} from 'utils.js'
const elementResizeDetectorMarker = require('element-resize-detector')
export default {
name: 'HelloWorld',
mixins: [resize],
data() {
return {
myChart: null
}
},
mounted() {
this.initChart()
const erd = elementResizeDetectorMarker()
const that = this
erd.listenTo(document.getElementById