vue中父組件向子組件傳值問題


問題:當父組件傳值給子組件echarts時,發現子組件獲取的props為空,剛開始以為是鈎子函數放錯了地方,后來發現從mounted和created都不行。當在父組件data定義傳遞的數據的時候子組件顯示正常

原因:后來經過排查,此處省略N字,發現echarts是在渲染的時候就傳遞數據

解決方案:在父組件定義一個flag,當數據獲得的之后再進行子組件的渲染

//父組件
   <div class="chart-wrapper">
    <pie-chart v-if="flag" :pie-data="piedata"></pie-chart>
  </div>
  ...
  import {getPie} from '@/api/status'
  export default {
  name: 'device',
  data() {
    return { 
      flag:false,
      piedata:{},
      ...
  },
  created(){
    this.init()
  },
 methods:{
   init(){   
        getPie().then(this.getInfoSucc)
   }, 
   getInfoSucc(res){
      res = res.data;
       if(res.code ==0){
         const values = res.values;  
         this.piedata = values.piedata;  
         this.flag = true 
       }
     }

---------------------

 https://blog.csdn.net/Uookic/article/details/80638883?utm_source=copy 

  

//子組件
<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { 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>

---------------------

https://blog.csdn.net/Uookic/article/details/80638883?utm_source=copy 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM