鍍金池/ 問答/HTML/ vue通過$ref獲取不到元素樣式?

vue通過$ref獲取不到元素樣式?

將元素樣式寫到style標簽內(nèi),通過$refs獲取到元素樣式全為空,
將元素樣式設置為行內(nèi)樣式,通過$refs可以獲取到。

如下代碼:

1.獲取不到樣式
<template>

<div class="tabbar" ref="tabbar">
    tabbar1
</div>

</template>

<script>

export default {
    data () {
        return {
            
        }
    },
    mounted () {
        console.log('he -- ',this.$refs.tabbar.style.height);
    }
}

</script>

<style lang="scss" scoped>
.tabbar {

position: fixed;
left: 0;
bottom: 0;
width: 100%;
border: 1px solid #ccc;
height: 0.98rem;

}
</style>

2.可以獲取到樣式
<template>

<div style="height: 0.98rem;" class="tabbar" ref="tabbar">
    tabbar1
</div>

</template>

<script>

export default {
    data () {
        return {
            
        }
    },
    mounted () {
        console.log('he -- ',this.$refs.tabbar.style.height);
    }
}

</script>

<style lang="scss" scoped>
.tabbar {

position: fixed;
left: 0;
bottom: 0;
width: 100%;
border: 1px solid #ccc;

}
</style>

哪位大俠知道不通過設置行內(nèi)樣式的方式,怎樣獲取到元素樣式?

回答
編輯回答
礙你眼

this.$refs.tabbar.style.offsetHeight);

2017年5月21日 12:53
編輯回答
尐懶貓

上面的情況

this.$refs.tabbar.height

下面的情況

this.$refs.tabbar.style.height
2018年3月7日 15:09
編輯回答
忠妾

您好,這個問題解決了么?

2018年1月9日 00:36