访问元素的属性时Vue $ refs未定义

问题描述

我有一个模态的元素部分,像这样:

<template>
    <portal to="portal-level-3">
        <div
            v-if="isOpen"
            class="fixed z-50 px-4 pb-4 inset-0 flex items-center justify-center"
            :class="[isContentHeightGreaterThanHolder ? 'items-baseline' : 'items-center']"
        >
            <div
                class="fixed inset-0 transition-opacity"
                enter-active-class="ease-out duration-300"
                enter-class="opacity-0"
                enter-to-class="scale-100"
                leave-active-class="ease-in duration-200"
                leave-class="opacity-100"
                leave-to-class="opacity-0"
            >
                <div class="absolute inset-0 bg-gray-500 opacity-75"></div>
            </div>

            <div
                v-if="isOpen"
                v-click-outside="closeModal"
                ref="refContent"
                :class="[
                    notFullWidth ? '' : 'w-full',modalWidthClass
                ]"
                class="bg-white rounded-lg overflow-hidden shadow-xl transform transition-all px-4 pt-5 pb-4"
                enter-active-class="ease-out duration-300"
                enter-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
                enter-to-class="opacity-100 translate-y-0 sm:scale-100"
                leave-active-class="ease-in duration-200"
                leave-class="opacity-100 translate-y-0 sm:scale-100"
                leave-to-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
            >

                <div class="flex mb-4">
                    <div v-if="title" class="flex-1">
                        <h3 class="text-lg leading-6 font-medium text-brand">
                            {{ title }}
                        </h3>
                    </div>

                    <div class="pl-3">
                        <button
                            @click.prevent="closeModal()"
                            type="button"
                            class="text-gray-400 hover:text-gray-500 focus:outline-none focus:text-gray-500 transition ease-in-out duration-150"
                        >
                            <font-awesome-icon class="h-6 w-6" :icon="['fal','times']" />
                        </button>
                    </div>
                </div>

                <slot></slot>

            </div>
        </div>
    </portal>

</template>

Vue实例中,我注意这种模态开口:

watch: {
    visible(val) {
        this.isOpen = val
    },isOpen(val) {
        if (val) {
            this.$nextTick(() => {
                let holderHeight = this.$el.offsetHeight
                let contentHeight = this.$refs.refContent.offsetHeight
                this.isContentHeightGreaterThanHolder = contentHeight >= (holderHeight - 30)
            })
        }
    }
},

但这会导致:

"TypeError: Cannot read property 'offsetHeight' of undefined"

但是如果我console.log(this.$refs)我会得到:

{}
refContent: div.bg-white.rounded-lg.overflow-hidden.shadow-xl.transform.transition-all.px-4.pt-5.pb-4.w-full.max-w-xl
@@clickoutsideContext: {id: 5,methodName: "closeModal",documentHandler: ƒ,bindingFn: ƒ}
accessKey: ""
align: ""
ariaAtomic: null
ariaAutoComplete: null
ariaBusy: null
ariaChecked: null
ariaColCount: null
ariaColIndex: null
ariaColSpan: null
ariaCurrent: null
ariaDescription: null
ariadisabled: null
ariaExpanded: null
....
offsetHeight: 805

为什么我不能访问this.$refs.refContent.offsetHeight?谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)