Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/templateRef.md
1---2category: Component3---45::: info6This function will be removed in future version.78Vue 3.5 introduced the `useTemplateRef` API which can effectively replace the functionality of `templateRef`, therefore we recommend using the native approach.9:::1011# templateRef1213Shorthand for binding ref to template element.1415## Usage1617<!-- eslint-skip -->1819```vue20<script lang="ts">21import { templateRef } from '@vueuse/core'2223export default {24setup() {25const target = templateRef('target')2627// no need to return the `target`, it will bind to the ref magically28},29}30</script>3132<template>33<div ref="target" />34</template>35```3637### With JSX/TSX3839```tsx40import { templateRef } from '@vueuse/core'4142export default {43setup() {44const target = templateRef<HTMLElement | null>('target', null)4546// use string ref47return () => <div ref="target"></div>48},49}50```5152### `<script setup>`5354There is no need for this when using with `<script setup>` since all the variables will be exposed to the template. It will be exactly the same as `ref`.5556```vue57<script setup lang="ts">58import { ref } from 'vue'5960const target = ref<HTMLElement | null>(null)61</script>6263<template>64<div ref="target" />65</template>66```6768## Type Declarations6970```ts71/**72* @deprecated Use Vue's built-in `useTemplateRef` instead.73*74* Shorthand for binding ref to template element.75*76* @see https://vueuse.org/templateRef77* @param key78* @param initialValue79*80* @__NO_SIDE_EFFECTS__81*/82export declare function templateRef<83T extends HTMLElement | SVGElement | Component | null,84Keys extends string = string,85>(key: Keys, initialValue?: T | null): Readonly<Ref<T>>86```87