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


免責聲明!

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



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