问题:当父组件传值给子组件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

  

vue中父组件向子组件传值问题的更多相关文章

  1. vue中父级与子组件生命周期的先后顺序

    1.vue的生命周期 2.views/createrCustormer.vue为父级     <template>     <expressService />   </ ...

  2. Vue中利用$emit实现子组件向父组件通信

    Vue中利用$emit实现子组件向父组件通信 父组件 <template> <div> <p>我是父组件</p> <child :isShow=& ...

  3. artdialog4.1.7 中父页面给子页面传值

    artdialog4.1.7中父页面给子页面传值时看了一些网友的解决方法: 在父页面声明全局变量 var returnValue=“ ”,子页面用art.dialog.opener.returnVal ...

  4. layer.open中父页面向子页面传值

    1.咱先看图说话 父list.jsp 子operate.jsp实现的代码1 在父页面上完成对子页面的数据渲染 function setData(data) { var lay=layer.open({ ...

  5. vue单文件组件形成父子(子父)组件之间通信(vue父组件传递数据给子组件,子组件传递数据给父组件)

    看了很多文章,官网文档也有看,对父子组件通信说的不是很明白:决定自己总结一下: vue一般都使用构建工具构建项目:这样每个组件都是单文件组件:而网上很多文章都是script标签方式映入vue,组件通信 ...

  6. vue-父组件向子组件传值

    一.父组件向子组件传值 其实该问题是说子组件如何访问父组件的属性和方法?那么根据对组件化的理解,无非就是要解决两个问题: 1.父组件如何将值传给子组件? 2.子组件如何获取父组件传递过来的值? 解读v ...

  7. Vue中父组件向子组件传值

    Vue中父组件向子组件传值 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  8. VUe.js 父组件向子组件中传值及方法

    父组件向子组件中传值 1.  Vue实例可以看做是大的组件,那么在其内部定义的私有组件与这个实例之间就出现了父子组件的对应关系. 2. 父子组件在默认的情况下,子组件是无妨访问到父组件中的数据的,所以 ...

  9. Vue中,父组件向子组件传值

    1:在src/components/child/文件夹下,创建一个名为:child.vue的子组件 2:在父组件中,设置好需要传递的数据 3:在App.vue中引入并注册子组件 4:通过v-bind属 ...

  10. Vue中的父组件给子组件传值

    父子组件传值: 父组件在调用子组件的地方,添加一个自定义的属性,属性的值就是你要传递给子组件的数据,如果值是一个变量,那么需要使用到绑定属性: 在子组件定义的地方,添加一个props选项,值为一个数组 ...

随机推荐

  1. Dubbo之服务注册

    在上一篇文章Dubbo之服务暴露分析中介绍了当远程暴露时,如果有注册中心,需要在服务暴露后再将服务注册到注册中心.该篇将介绍该功能的有关步骤. 注册的起点 在RegistryProtocol.expo ...

  2. 将HTML保存为PDF

    使用的是   jsPDF 引用 <script src="https://code.jquery.com/jquery-git.js"></script> ...

  3. 怎么压缩PPT大小?

    PPT体积过大有几个原因: 1.母版内版式过多.解决方法:①点击“视图”选项卡下的“幻灯片母版”:②删除左边没必要的版式. 2.图片质量太大.解决方法:①选中任意一张图片:②点击“图片工具”的“格式” ...

  4. C++-蓝桥杯-入门训练

    Fibonacci数列,快速幂 #include <cstdio> ][];}; ,MOD=; Matrix A,B,O,I; Matrix Mul(Matrix A,Matrix B){ ...

  5. D - Three Integers

    https://codeforces.com/contest/1311/problem/D 本题题意:给出a,b,c三个数,a<=b<=c: 可以对三个数中任意一个进行+1或-1的操作: ...

  6. [HAOI2011] Problem b - 莫比乌斯反演

    复习一下莫比乌斯反演 首先很显然用一下容斥把它转化成求 \(ans=\sum_{i=1}^a \sum_{j=1}^b [{gcd(i,j)=d}]\) 我们可以定义 f(d) 和 F(d) 如下: ...

  7. Session方法

    Session的save()和persist()方法Session的save()方法使一个临时对象转变为持久化对象.它完成以下操作:(1)将临时对象加入到Session缓存中,使其进入持久化状态.(2 ...

  8. Java 13 新特性

    一.core-libs/java.nio 添加 FileSystems.newFileSystem(Path, Map<String, ?>) 方法 java.nio.file.FileS ...

  9. linux学习笔记1:无操作系统时LED驱动

  10. [LOJ113] 最大异或和 - 线性基

    虽然是SB模板但还真是第一次手工(然而居然又被运算符优先级调戏了) #include <bits/stdc++.h> using namespace std; #define int lo ...