Template refs let you get a direct link to an HTML element or component in your Vue template. This helps you interact with the element in your code, like focusing an input or measuring size.
Template refs for DOM access in Vue
<template> <element ref="myRef"></element> </template> <script setup> import { ref } from 'vue' const myRef = ref(null) // Access the element with myRef.value </script>
The ref attribute in the template names the element or component.
In the script, you create a ref variable with null initial value.
<template> <input ref="inputBox" /> </template> <script setup> import { ref, onMounted } from 'vue' const inputBox = ref(null) onMounted(() => { inputBox.value.focus() }) </script>
<template> <div ref="box">Hello</div> </template> <script setup> import { ref, onMounted } from 'vue' const box = ref(null) onMounted(() => { console.log(box.value.textContent) }) </script>
<template> <ChildComponent ref="childComp" /> </template> <script setup> import { ref } from 'vue' import ChildComponent from './ChildComponent.vue' const childComp = ref(null) function callChildMethod() { childComp.value.someMethod() } </script>
This component has an input box and a button. Clicking the button focuses the input box using a template ref. The input value is shown below and updates as you type.
<template> <input ref="nameInput" placeholder="Type your name" @input="updateValue" /> <button @click="focusInput">Focus Input</button> <p>Input value: {{ inputValue }}</p> </template> <script setup> import { ref } from 'vue' const nameInput = ref(null) const inputValue = ref('') function focusInput() { nameInput.value.focus() } // Update inputValue when input changes function updateValue() { inputValue.value = nameInput.value.value } </script> <style scoped> input { padding: 0.5rem; font-size: 1rem; margin-bottom: 0.5rem; display: block; } button { padding: 0.5rem 1rem; font-size: 1rem; cursor: pointer; } </style>
Template refs are only available after the component is mounted.
Access the DOM element with refVariable.value.
Use template refs carefully to avoid tightly coupling your code to the DOM structure.
Template refs let you directly access DOM elements or child components in Vue templates.
Use ref attribute in the template and a ref variable in the script.
They are useful for focusing inputs, reading element properties, or calling child component methods.
Practice
template refs in Vue?Solution
Step 1: Understand what template refs do
Template refs provide a way to directly access DOM elements or child components inside Vue templates.Step 2: Compare with other Vue features
Reactive state variables and computed properties are for data management, not direct DOM access. Global components registration is unrelated.Final Answer:
To directly access DOM elements or child components from the template -> Option AQuick Check:
Template refs = direct DOM/component access [OK]
- Confusing refs with reactive state
- Thinking refs create computed properties
- Assuming refs register components
<script setup> syntax?Solution
Step 1: Recall how to declare refs in Vue 3 script setup
Useconst myRef = ref(null);to create a ref variable initialized to null.Step 2: Check other options for correctness
reactiveis for objects, not refs.computedis for derived values. Usingvaris discouraged and 'element' is not a valid initial value for a ref to DOM.Final Answer:
const myRef = ref(null); -> Option AQuick Check:
Declare ref with ref(null) [OK]
- Using reactive instead of ref for DOM refs
- Initializing ref with wrong value
- Using var instead of const
<template>
<input ref="inputRef" />
<button @click="focusInput">Focus</button>
</template>
<script setup>
import { ref } from 'vue';
const inputRef = ref(null);
function focusInput() {
console.log(inputRef.value.tagName);
inputRef.value.focus();
}
</script>Solution
Step 1: Understand what inputRef.value refers to
inputRef is a ref to the input DOM element, so inputRef.value is the actual input element.Step 2: Check what tagName returns and focus behavior
inputRef.value.tagName returns the uppercase tag name "INPUT". Calling focus() sets keyboard focus on the input.Final Answer:
"INPUT" will be logged and the input will get focus -> Option CQuick Check:
inputRef.value.tagName = "INPUT" and focus works [OK]
- Logging the ref variable name instead of its value
- Forgetting to use .value to access the element
- Assuming tagName is lowercase
<template>
<div ref="box">Content</div>
</template>
<script setup>
import { ref } from 'vue';
const box = ref();
console.log(box.value.textContent);
</script>Solution
Step 1: Check ref initialization and usage timing
box is declared as ref() without initial value, which defaults to undefined. Accessing box.value.textContent immediately runs before DOM is ready.Step 2: Understand Vue lifecycle for DOM refs
Template refs are assigned after the component mounts. Accessing them too early (during setup) causes box.value to be null or undefined, so textContent access fails.Final Answer:
Accessing box.value.textContent before DOM is mounted causes error -> Option BQuick Check:
Access DOM refs only after mount [OK]
- Trying to read ref value before DOM exists
- Not initializing ref with null (less critical)
- Confusing ref attribute syntax
Solution
Step 1: Use onMounted to run code after DOM is ready
To safely access the input element, focus must be called inside onMounted lifecycle hook.Step 2: Access the DOM element via inputRef.value and call focus()
inputRef is a ref initialized to null, and inputRef.value holds the DOM element after mount. Calling inputRef.value.focus() sets focus correctly.Final Answer:
Code snippet C correctly uses onMounted and inputRef.value.focus() -> Option DQuick Check:
Use onMounted + inputRef.value.focus() [OK]
- Calling focus outside onMounted before DOM ready
- Using :ref instead of ref attribute
- Calling focus on ref instead of ref.value
Ready to prove it?
Pass the test and this pattern moves unknown
