首页 > 其他 > 详细

父组件数据改变,子组件ECharts数据重新渲染

时间:2020-05-21 11:14:08      阅读:384      评论:0      收藏:0      [点我收藏+]

没啥好说的  直接贴代码⑧

父组件

<div>
      <employee-e-charts v-if="degreeData" :id="‘degree‘" :chart-data="degreeData" :width="‘600px‘" />
</div>

子组件

<template>
  <div>
    <div :id="id" :style="{width: width, height: height}" />
  </div>
</template>

<script>
import echarts from ‘echarts‘

export default {
  name: ‘EmployeeECharts‘,
  props: {
    width: {
      type: String,
      default: ‘400px‘
    },
    height: {
      type: String,
      default: ‘300px‘
    },
    chartData: {
      type: Object,
      required: true
    },
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      chart: ‘‘
    }
  },
  watch: {
    chartData(val) {
      this.setOptions(val)
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    async init() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.setOptions(this.chartData)
    },
    setOptions({ reportName, employeeX, employeeY } = {}) {
      this.chart.setOption({
        title: {
          text: reportName
        },
        tooltip: {},
        legend: {
          data: employeeX
        },
        xAxis: {
          data: employeeX
        },
        yAxis: {},
        series: [{
          type: ‘bar‘,
          data: employeeY,
          label: {
            normal: {
              show: true,
              position: ‘top‘
            }
          }
        }]
      })
    }
  }
}
</script>

<style scoped>

</style>

重点是子组件里面的watch

以上

父组件数据改变,子组件ECharts数据重新渲染

原文:https://www.cnblogs.com/pjw233/p/12928989.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!