A template ref is a way to directly access a DOM element or a component instance inside the Vue template using a special attribute called ref. It helps you interact with the element or component in your script.
Click to reveal answer
beginner
How do you define a template ref on an element in Vue 3 Composition API?
You add <code>ref="name"</code> to the element in the template, then in the script use <code>const name = ref(null)</code> and Vue will link the DOM element to this variable after rendering.
Click to reveal answer
intermediate
Why should you avoid manipulating the DOM directly in Vue?
Vue manages the DOM for you reactively. Direct DOM manipulation can cause conflicts or unexpected behavior. Template refs should be used carefully and mostly for things Vue can't handle, like focusing an input.
Click to reveal answer
beginner
How do you access a DOM element using a template ref in Vue 3?
After defining a ref in the template and script, you access the DOM element with name.value. For example, inputRef.value.focus() sets focus on the input element.
Click to reveal answer
intermediate
What is the difference between ref and reactive in Vue?
ref creates a reactive reference to a single value or DOM element, accessed with .value. reactive creates a reactive object with multiple properties. Template refs use ref for DOM access.
Click to reveal answer
How do you declare a template ref for a DOM element in Vue 3 Composition API?
AUse <code>ref="name"</code> in template and <code>const name = ref(null)</code> in script
BUse <code>v-model="name"</code> in template and <code>const name = reactive({})</code> in script
CUse <code>id="name"</code> in template and <code>document.getElementById('name')</code> in script
DUse <code>ref="name"</code> in template and <code>const name = reactive(null)</code> in script
✗ Incorrect
Template refs require the ref attribute in the template and a matching ref variable in the script initialized to null.
What is the correct way to access the DOM element from a template ref named inputRef?
A<code>inputRef.value</code>
B<code>inputRef</code>
C<code>inputRef.dom</code>
D<code>inputRef.element</code>
✗ Incorrect
In Vue 3, refs are accessed with .value to get the actual DOM element or value.
When is it appropriate to use template refs in Vue?
ATo replace all reactive data binding
BTo declare global variables
CTo style elements with CSS
DTo directly manipulate DOM elements when Vue cannot handle it
✗ Incorrect
Template refs are used for direct DOM access when needed, like focusing inputs, but not for general reactive data.
Which Vue API is used to create a reactive reference for template refs?
Acomputed()
Breactive()
Cref()
Dwatch()
✗ Incorrect
ref() creates a reactive reference suitable for template refs.
What will happen if you try to access a template ref before the component is mounted?
AIt will return the DOM element immediately
BIt will be null or undefined
CIt will throw a syntax error
DIt will automatically mount the component
✗ Incorrect
Template refs are only set after the component is mounted, so before that they are null.
Explain how to use template refs in Vue 3 to focus an input element after the component loads.
Think about linking template and script with ref, then using lifecycle to access DOM.
You got /3 concepts.
Describe the difference between using template refs and reactive data in Vue.
Consider when you want to change the UI vs when you want to reach the actual element.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using template refs in Vue?
easy
A. To directly access DOM elements or child components from the template
B. To define reactive state variables
C. To create computed properties
D. To register global components
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 A
Quick Check:
Template refs = direct DOM/component access [OK]
Hint: Template refs connect template elements to script variables [OK]
Common Mistakes:
Confusing refs with reactive state
Thinking refs create computed properties
Assuming refs register components
2. Which of the following is the correct way to declare a template ref in Vue 3's <script setup> syntax?
easy
A. const myRef = ref(null);
B. let myRef = reactive(null);
C. const myRef = computed(() => null);
D. var myRef = ref('element');
Solution
Step 1: Recall how to declare refs in Vue 3 script setup
Use const myRef = ref(null); to create a ref variable initialized to null.
Step 2: Check other options for correctness
reactive is for objects, not refs. computed is for derived values. Using var is discouraged and 'element' is not a valid initial value for a ref to DOM.
Final Answer:
const myRef = ref(null); -> Option A
Quick Check:
Declare ref with ref(null) [OK]
Hint: Use ref(null) to declare template refs in script setup [OK]
Common Mistakes:
Using reactive instead of ref for DOM refs
Initializing ref with wrong value
Using var instead of const
3. Given the following Vue 3 component code, what will be logged when the button is clicked?
A. The ref variable 'box' is not initialized with null
B. Accessing box.value.textContent before DOM is mounted causes error
C. The ref attribute should be used as :ref instead of ref
D. Missing import of onMounted to access DOM safely
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 B
Quick Check:
Access DOM refs only after mount [OK]
Hint: Access refs after mount, not immediately in setup [OK]
Common Mistakes:
Trying to read ref value before DOM exists
Not initializing ref with null (less critical)
Confusing ref attribute syntax
5. You want to create a Vue 3 component that automatically focuses an input element when the component is mounted using template refs. Which code snippet correctly implements this behavior?