怎么才能正确的获取到 el-dialog 中的组件 ref?

新手上路,请多包涵

按照 https://element-plus.gitee.io... 说的,我使用了 "openDialog" 函数获取 "refCompContent",代码类似:

<template>
    <el-dialog ref="refDialog" @open="openDialog">
        <component :is="compContent" ref="refCompContent"/>
    </el-dialog>
</template>

<script>
    const refCompContent = ref(null)
    const openDialog = () => {
        console.log('>>> refCompContent::', refCompContent)
    }
</script>

但 "refCompContent" 是 null,怎么才能正确的获取到 "refCompContent"?谢谢

阅读 5k
3 个回答

const refDialog = ref(null)
// ref 引用名称 跟template里面要保持一致

改成这样试试


    const openDialog = () => {
const refCompContent = ref(null)
        console.log('>>> refCompContent::', refCompContent)
    }
新手上路,请多包涵

你可是把el-dialog打开后再获取refCompContent,否则是会null,因为在没打开dialog之前,在页面查看源码的时候,dialog的div是没有的,类似于v-if。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题