登录  /  注册

vue中父组件向子组件echarts传值问题

jacklove
发布: 2018-06-11 22:21:09
原创
4034人浏览过

记录echarts踩坑问题

问题:当父组件传值给子组件echarts时,发现子组件获取的props为空,刚开始以为是钩子函数放错了地方,后来发现从mounted和created都不行。当在父组件data定义传递的数据的时候子组件显示正常

原因:后来经过排查,此处省略N字,发现echarts是在渲染的时候就传递数据

解决方案:在父组件定义一个flag,当数据获得的之后再进行子组件的渲染

//父组件
   <p class="chart-wrapper">
    <pie-chart v-if="flag" :pie-data="piedata"></pie-chart>
  </p>  ...
  export default {
  name: &#39;device&#39;,
  data() {    return { 
      flag:false,
      piedata:{},      ...
  },
  created(){
    this.init()
  },
 methods:{
   init(){   
       axios.get(&#39;/static/mock/status/pie.json&#39;).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 &#39;echarts&#39;require(&#39;echarts/theme/macarons&#39;) // echarts themeimport { debounce } from &#39;@/utils&#39;export default {
  props: {
    pieData: {
      type: Object
    },
    msg: {
      type:Number
    },
    className: {
      type: String,      default: &#39;chart&#39;
    },
    width: {
      type: String,      default: &#39;100%&#39;
    },
    height: {
      type: String,      default: &#39;300px&#39;
    }
  },
  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(&#39;resize&#39;, this.__resizeHanlder) 
  },
  beforeDestroy() {    if (!this.chart) {      return
    }
    window.removeEventListener(&#39;resize&#39;, this.__resizeHanlder)    this.chart.dispose()    this.chart = null
  },
  methods: {
    setOptions({ text, arrtype, arrdata } = {}) {  
      this.chart.setOption({
        title: {
          text: text
        },
        tooltip: {
          trigger: &#39;item&#39;,
          formatter: &#39;{a} <br/>{b} : {c} ({d}%)&#39;
        },
        legend: {
          left: &#39;center&#39;,
          bottom: &#39;10&#39;,
          data: arrtype
        },
        calculable: true,
        series: [
          {
            name: &#39;&#39;,
            type: &#39;pie&#39;,
            roseType: &#39;radius&#39;,
            radius: [15, 95],
            center: [&#39;50%&#39;, &#39;42%&#39;],
            data: arrdata,
            animationEasing: &#39;cubicInOut&#39;,
            animationDuration: 2600
          }
        ]
      })
    },
    initChart() {      this.chart = echarts.init(this.$el, &#39;macarons&#39;)      this.setOptions(this.pieData); 
    }
  }
}</script>
登录后复制

然后子组件就能正常显示了
这里写图片描述

本文讲解了vue中父组件向子组件echarts传值问题 ,更多相关内容请关注php中文网。

相关推荐:
Javascript 严格模式详解

php实现登录功能的相关代码解析

JavaScript相关的内容讲解

以上就是vue中父组件向子组件echarts传值问题的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号