<template>
    <div id="dom1" class="charts-box"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
    name: 'ChartBar',
    props: {
        chartOption: {
            type: Object,
            default() {
                return null
            }
        }
    },
    data() {
        return {
            myChart: null
        }
    },
    watch: {
        chartOption: {
            handler(params) {
                if (params) {
                    this.$nextTick(() => {
                        
                        this.setOption(params)
                    })
                }
            },
            deep: true,
            immediate: true
        }
    },
    mounted() {
        this.$nextTick(() => {
            if (this.chartOption) {
                this.initChart()
            }
        })
    },
    beforeDestroy() {},
    methods: { 
        setOption(params) {
            if (!this.myChart) {
                console.log("开始dom1")
                this.myChart = echarts.init(document.getElementById('dom1'))
                console.log("开始dom2")
            }
            this.myChart.setOption(params)
        },
        resize() {
            this.myChart.resize()
        },
        initChart(params) {
            this.$nextTick(() => {
                this.setOption(this.chartOption)
            })
        }
    }
}
</script>
<style lang="scss" scoped>
.charts-box {
    width: 100%;
    height: 100%;
}
</style>
