unrefElement
从 Vue ref 或组件实例中检索底层 DOM 元素
用法
vue
<script setup>
import { unrefElement } from '@vueuse/core'
import { onMounted, useTemplateRef } from 'vue'
const div = useTemplateRef<HTMLElement>('div') // will be bound to the <div> element
const hello = useTemplateRef<Component>('hello') // will be bound to the HelloWorld Component
onMounted(() => {
console.log(unrefElement(div)) // the <div> element
console.log(unrefElement(hello)) // the root element of the HelloWorld Component
})
</script>
<template>
<div ref="div" />
<HelloWorld ref="hello" />
</template>
类型声明
typescript
export type VueInstance = ComponentPublicInstance
export type MaybeElementRef<T extends MaybeElement = MaybeElement> = MaybeRef<T>
export type MaybeComputedElementRef<T extends MaybeElement = MaybeElement> =
MaybeRefOrGetter<T>
export type MaybeElement =
| HTMLElement
| SVGElement
| VueInstance
| undefined
| null
export type UnRefElementReturn<T extends MaybeElement = MaybeElement> =
T extends VueInstance ? Exclude<MaybeElement, VueInstance> : T | undefined
/**
* Get the dom element of a ref of element or Vue component instance
*
* @param elRef
*/
export declare function unrefElement<T extends MaybeElement>(
elRef: MaybeComputedElementRef<T>,
): UnRefElementReturn<T>
源代码
贡献者
更新日志
v12.8.0
于 2025/3/5v12.3.0
于 2025/1/259f75
- feat(toValue): 弃用 @vueuse/shared
中的 toValue
,改用 Vue 原生方法v12.0.0-beta.1
于 2024/11/21v10.0.0-beta.4
于 2023/4/134d757
- feat(types)!: 将 MaybeComputedRef
重命名为 MaybeRefOrGetter
0a72b
- feat(toValue): 将 resolveUnref
重命名为 toValue