Skip to content

why useTemplateRef() can not get element in onMounted hook, the value will be null #13269

Discussion options

You must be logged in to vote

You need to declare your template ref outside of the hook:

<script setup lang="ts">
import { onMounted, useTemplateRef } from 'vue'

const title = useTemplateRef('title')

onMounted(() => {
  console.log(title.value)
})
</script>

<template>
  <h1 ref="title">Vue3.5</h1>
</template>

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@DiHaozhe-365
Comment options

Answer selected by DiHaozhe-365
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants