跳到内容

unrefElement

分类
导出大小
152 B
最后更改
5 天前

从 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>

源代码

SourceDocs

贡献者

Anthony Fu
IlyaL
Anthony Fu
Henrik Kampshoff
Nebula
Julian Meinking
Jelf

更新日志

v12.8.0 于 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,改用 Vue 原生方法 (#4636)
v12.3.0 于 2025/1/2
59f75 - feat(toValue): 弃用 @vueuse/shared 中的 toValue,改用 Vue 原生方法
v12.0.0-beta.1 于 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化包大小并清理代码 (#4349)
v10.0.0-beta.4 于 2023/4/13
4d757 - feat(types)!: 将 MaybeComputedRef 重命名为 MaybeRefOrGetter
0a72b - feat(toValue): 将 resolveUnref 重命名为 toValue

根据 MIT 许可证发布。