why useTemplateRef() can not get element in onMounted hook, the value will be null #13269
Answered by Deckluhm
DiHaozhe-365 asked this question in Help/Questions
-
|
Beta Was this translation helpful? Give feedback.
Answered by Deckluhm May 2, 2025
Replies: 1 comment 1 reply
-
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> |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by DiHaozhe-365
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You need to declare your template ref outside of the hook: