vue父组件异步获取动态数据传递给子组件 ,子组件获取不到值或者延时获取

如果父组件中要传给子组件的值是从接口里面获取的,那么子组件刚开始得到的是空值。这个时候不应该传给子组件的。你可以在子组件里面延时加载。或者是按照下面方法来

父组件:


<template>

       <div>

              <chartpie  :chartValue="chartValue" v-if="chartValue.length>0"></chartpie>   //因为chartValue是个数组。判断数组长度大于0就是获取到了接口里面传来的数据。然后子组件一定可以获取到异步的那个值

     </div>
</template>

<script>

import chartpie  from '@/components/Chartpie'
export default {
   components: {chartpie},
   data(){
       return{

            chartValue: [],

         }

      },

 methods: {

      getUserreport() {
                getCpreport({id:id}).then(res => {

                    this.chartValue=res.data;

                 })

            },

},

 mounted() {
        this.getUserreport();
  },
}
</script>

子组件

<template>

<div>{{chartValue}}</div> 

</template>

export default {

    data() {
        return {
                
        }
    },    
    props: [ 'chartValue'],

}

猜你喜欢

转载自blog.csdn.net/qq_33769914/article/details/86158733