0
0
Vueframework~10 mins

Template refs for DOM access in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Template refs for DOM access
Template renders
Ref attribute set on element
Vue creates ref object
ref.value points to DOM element
Access DOM via ref.value in script
Manipulate or read DOM element
Vue renders the template, sets the ref attribute, creates a ref object, and links ref.value to the DOM element for script access.
Execution Sample
Vue
<template>
  <button ref="btnRef">Click me</button>
</template>

<script setup>
import { ref, onMounted } from 'vue'
const btnRef = ref(null)
onMounted(() => {
  btnRef.value.focus()
})
</script>
This code sets a template ref on a button and focuses it when the component mounts.
Execution Table
StepActionRef Value BeforeRef Value AfterEffect
1Component starts renderingnullnullNo DOM yet
2Template creates <button> with ref="btnRef"nullnullButton element created but ref not linked yet
3Vue links btnRef.value to button DOM elementnull<button>btnRef.value now points to button
4onMounted hook runs<button><button>btnRef.value.focus() called
5Button receives focus<button><button>Button is focused in browser
💡 Component fully mounted, ref linked, and DOM accessed
Variable Tracker
VariableStartAfter Step 3After Step 4Final
btnRef.valuenull<button><button><button>
Key Moments - 3 Insights
Why is btnRef.value null before the template renders?
Before rendering, the DOM element does not exist, so btnRef.value is null as shown in execution_table step 1 and 2.
How does Vue link the ref to the actual DOM element?
Vue sets btnRef.value to the DOM element after creating it in the template, as seen in execution_table step 3.
Why can we call btnRef.value.focus() safely inside onMounted?
Because onMounted runs after the DOM is rendered and btnRef.value is linked, so the button element exists (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is btnRef.value at step 2?
Aundefined
B<button>
Cnull
Da string 'btnRef'
💡 Hint
Check the 'Ref Value After' column at step 2 in execution_table
At which step does btnRef.value first point to the button DOM element?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for when 'Ref Value After' changes from null to
If we tried to call btnRef.value.focus() before step 3, what would happen?
AAn error or no effect because btnRef.value is null
BbtnRef.value would be undefined
CThe button would be focused successfully
DThe button would be clicked automatically
💡 Hint
Refer to variable_tracker and execution_table steps 1 and 2 where btnRef.value is null
Concept Snapshot
Template refs let you access DOM elements in Vue templates.
Use ref="name" on an element.
In script, create a ref variable with ref(null).
After rendering, ref.value points to the DOM element.
Use onMounted to safely access DOM via ref.value.
Useful for focus, measurements, or direct DOM manipulation.
Full Transcript
In Vue, template refs let you get direct access to DOM elements. You add ref="btnRef" on a button in the template. In the script, you create btnRef with ref(null). When Vue renders the template, it creates the button element but btnRef.value is still null until Vue links it. After linking, btnRef.value points to the button DOM element. You can then call methods like btnRef.value.focus() safely inside the onMounted hook because the DOM is ready. This process helps you interact with the DOM directly in a controlled way.