Record the problem of echarts stepping on the trap
#Problem: When the parent component passes the value to the child component echarts, it is found that the child component obtains The props were empty. At first I thought the hook function was in the wrong place, but later I found that neither mounted nor created worked. When the data passed in the parent component data is defined, the child component displays normally
Cause: After investigation later, the N word was omitted here, and it was found that echarts passes the data during rendering
Solution Solution: Define a flag in the parent component, and then render the subcomponent after the data is obtained
//父组件 <p class="chart-wrapper"> <pie-chart v-if="flag" :pie-data="piedata"></pie-chart> </p> ... export default { name: 'device', data() { return { flag:false, piedata:{}, ... }, created(){ this.init() }, methods:{ init(){ axios.get('/static/mock/status/pie.json').then(this.getInfoSucc) }, getInfoSucc(res){ res = res.data; if(res.code ==0){ const values = res.values; this.piedata = values.piedata; this.flag = true } }
//子组件<template> <p :class="className" :style="{height:height,width:width}"></p></template><script>import echarts from 'echarts'require('echarts/theme/macarons') // echarts themeimport { debounce } from '@/utils'export default { props: { pieData: { type: Object }, msg: { type:Number }, className: { type: String, default: 'chart' }, width: { type: String, default: '100%' }, height: { type: String, default: '300px' } }, data() { return { chart: null } }, // watch: { // piedata: { // deep: true, // handler(val) { // console.log(val) // this.setOptions(val) // } // } // }, mounted() { console.log("pieData:"+JSON.stringify(this.pieData)) this.initChart() this.__resizeHanlder = debounce(() => { if (this.chart) { this.chart.resize() } }, 100) window.addEventListener('resize', this.__resizeHanlder) }, beforeDestroy() { if (!this.chart) { return } window.removeEventListener('resize', this.__resizeHanlder) this.chart.dispose() this.chart = null }, methods: { setOptions({ text, arrtype, arrdata } = {}) { this.chart.setOption({ title: { text: text }, tooltip: { trigger: 'item', formatter: '{a} <br/>{b} : {c} ({d}%)' }, legend: { left: 'center', bottom: '10', data: arrtype }, calculable: true, series: [ { name: '', type: 'pie', roseType: 'radius', radius: [15, 95], center: ['50%', '42%'], data: arrdata, animationEasing: 'cubicInOut', animationDuration: 2600 } ] }) }, initChart() { this.chart = echarts.init(this.$el, 'macarons') this.setOptions(this.pieData); } } }</script>
Then the subcomponent will be displayed normally
This article explains There is a problem in passing values from the parent component to the child component echarts in Vue. For more related content, please pay attention to the PHP Chinese website.
Related recommendations:
Javascript strict mode detailed explanation
php related code analysis to implement login function
JavaScript related content explanation
The above is the detailed content of Problem in passing value from parent component to child component echarts in Vue. For more information, please follow other related articles on the PHP Chinese website!