zoukankan      html  css  js  c++  java
  • 父组件数据改变,子组件ECharts数据重新渲染

    没啥好说的  直接贴代码⑧

    父组件

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

    子组件

    <template>
      <div>
        <div :id="id" :style="{ width, height: height}" />
      </div>
    </template>
    
    <script>
    import echarts from 'echarts'
    
    export default {
      name: 'EmployeeECharts',
      props: {
         {
          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

    以上

  • 相关阅读:
    CVS,GIT,Mercurial和SVN比较
    ubuntu-使用终端配置网络
    编写简单的hashCode方法
    编写高质量equals方法
    文件上传和下载
    Java常用命令
    增删查改-MySQL
    Newton迭代法-C++
    二分法-C++
    适配器模式
  • 原文地址:https://www.cnblogs.com/pjw233/p/12928989.html
Copyright © 2011-2022 走看看