父组件数据改变,子组件ECharts数据重新渲染
没啥好说的 直接贴代码⑧
父组件
<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
以上
浙公网安备 33010602011771号