鍍金池/ 問答/C++  HTML/ 在vue中使用refs獲取對象的高度,寬度為0

在vue中使用refs獲取對象的高度,寬度為0

在vue模板中,控制子元素(echarts圖表組件)和父元素(div)的大小相等。在父元素的mounted鉤子中根據(jù)refs獲取父元素(div)的高度和寬度傳遞給子元素,可是每次在mounted打印出的高度都是0代碼如下:

<template>
<div class="pie" ref="monthPie">
   <v-colorPie :domHeight="domHeight"/>
</div>
</template>
<script>
export default {
    data(){
        return{
            domHeight:0
        }
    },
     mounted(){
         alert(this.$refs.monthPie.offsetHeight)
     }
}
</script>
<style>
.pie{
    margin-top:1%
    height:93%
    width:100%
}
</style>
回答
編輯回答
淡墨

你要看這個時候子組件是否加載完成

2017年1月4日 18:55
編輯回答
下墜

請問這個問題解決了么 同遇到

2018年9月20日 08:35
編輯回答
陌如玉

這種問題的思路是,你可以在子組件mounted鉤子中使用

//子組件
this.$nextTick(() => { //使用nextTick為了保證dom元素都已經(jīng)渲染完畢 
    this.$emit('eventGetHeight',this.$el.offsetHeight);
});

父組件中監(jiān)聽eventGetHeight事件,并取得這個數(shù)據(jù)

2018年9月18日 21:53