vue中使用echarts(含自适应监听及移除)

时间:2024-12-19 16:49:05
<script> export default { name: 'BarItemCharts', data () { return { chartIns: '', options: {} } }, mounted () { this.init() window.addEventListener('resize', this.onResize) }, methods: { init () { this.options = {...} // 此处写echarts配置 if (!this.chartIns) { this.chartIns = this.$echarts.init(this.$refs.canvas) } this.chartIns.setOption(this.options) }, onResize () { // 刚方法是为了确保addEventListener和removeEventListener的东西是同一个 if (this.chartIns) { this.chartIns.resize() } } }, beforeDestroy () { window.removeEventListener('resize', this.onResize) if (this.chartIns) { this.chartIns.dispose() } } } </script>