在 vue 腳手架開發中,echarts圖表自適應容器的方法:
父組件:
<template>
<div class="statistics_wrap">
<v-fir-grade ref="first"></v-fir-grade>
</div>
</template>
<script>
import vFirGrade from "./FirstGrade/index"
export default{
components: {
vFirGrade
},
data() {
return {
}
},
mounted() {
// 監聽窗口的變化,實時調用 echarts的 resize事件
window.onresize = () =>{
this.$refs.first.myChart.resize();
}
},
}
</script>
子組件:
<template>
<div class="_statistics_echarts_wrap">
<div id="echartCont"></div>
</div>
</template>
<script>
export default{
data() {
return {
myChart: null,
}
},
mounted() {
this.firstGrade();
},
methods: {
firstGrade() {
this.myChart = echarts.init(document.getElementById('echartCont'));
// ...
this.myChart.setOption(option);
}
}
}
</script>
