vue中子组件的created、mounted钩子中获取不到props中的值问题

父子组件通信

这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可 
例如: 
父组件中:

<template>
    <div>
        <head-top></head-top>
        <section class="data_section">
            <header class="chart-title">数据统计</header>
            <el-row :gutter="20" class="chart-head">
                <el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head blue-head">统计:</div></el-col>
                <el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head">销售数量 <span>{{number}}</span></div></el-col>
                <el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head">销售金额 <span>{{amount}}</span></div></el-col>
                <el-col :xs="24" :sm="12" :md="6" :lg="6"><div class="grid-content data-head">利润统计 <span>{{profits}}</span></div></el-col>
            </el-row>
        </section>
        <chart :chartData="chartData"></chart>
    </div>
</template>

<script>
    data(){
            return {
                number: null,
                amount: null,
                profits: null,
                chartData: [10,10,10]
            }
        },
</script>

子组件中:

export default {
    props: ['chartData']
}

这种情况下,子组件的 methods 中想要取到props中的值,直接使用 this.chartData 即可 
但是有写情况下,你的 chartData 里面的值并不是固定的,而是动态获取的,这种情况下,你会发现 methods 中是取不到你的 chartData 的,或者取到的一直是默认值。

比如下面这个情况 
父组件中:

<script>
    data(){
            return {
                number: null,
                amount: null,
                profits: null,
                chartData: []
            }
        },
        mounted(){
            this.getStatistics();
        },
        methods: {
            //获取统计数据
            getStatistics(){
                console.log('获取统计数据')
                axios.post(api,{

                }).then((res) => {
                    this.number = res.data.domain.list[0].number;
                    this.amount = res.data.domain.list[0].amount;
                    this.profits = res.data.domain.list[0].profits;
                    this.chartData = [this.number,this.amount,this.profits];
                }).catch((err) => {
                    console.log(err);
                })
            },
        },
</script>

此时子组件的methods中使用 this.chartData 会发现是不存在的(因为为空了)

这情况我是使用watch处理:

解决方法如下:

使用 watch :

export default {
    props: ['chartData'],
        data(){
            return {
                cData: []
            }
        },
        watch: {
            //正确给 cData 赋值的 方法
            chartData: function(newVal,oldVal){
                this.cData = newVal;  //newVal即是chartData
                newVa l&& this.drawChart(); //newVal存在的话执行drawChar函数
            }
        },
        methods: {
            drawChart(){
                //执行其他逻辑
            }
        },
      mounted() {
//在created、mounted这样的生命周期, 给 this.cData赋值会失败,错误赋值方法 // this.cData = this.chartData; } }

监听 chartData 的值,当它由空转变时就会触发,这时候就能取到了,拿到值后要做的处理方法也需要在 watch 里面执行。

//总结
出现这种情况的原因, 因为父组件中的要就要传递的  props  属性 是通过 发生ajax请求回来的, 请求的这个过程是需要时间的,但是子组件的渲染要快于ajax请求过程,所以此时  created 、 mounted  这样的只会执行一次的生命周期钩子,已经执行了,但是 props 还没有流进来(子组件),所以只能拿到默认值。

转载:https://blog.csdn.net/zmkyf1993/article/details/80320802
posted @ 2019-08-26 16:10  暗恋桃埖源  阅读(25736)  评论(2编辑  收藏  举报