📖 Scenario: You are building a simple Vue 3 app where you want to access a DOM element directly to change its style when a button is clicked. This is useful when you want to interact with the page elements without using data binding.
🎯 Goal: Create a Vue 3 component that uses a template ref to access a paragraph element and change its text color to blue when a button is clicked.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a template ref named textRef to access the paragraph element
Add a button that triggers a function to change the paragraph's text color
Use the ref function from Vue to create the template ref
Modify the paragraph's style directly using the template ref
💡 Why This Matters
🌍 Real World
Direct DOM access with template refs is useful when you need to manipulate elements that are not easily handled by reactive data, such as focusing inputs, measuring sizes, or applying styles dynamically.
💼 Career
Understanding template refs is important for Vue developers to handle edge cases and improve user experience by controlling DOM elements directly when needed.
Progress0 / 4 steps
1
Create the paragraph element with a template ref
Write the Vue 3 template code to create a paragraph element with the text "Hello, Vue!" and add a template ref named textRef to it.
Vue
Hint
Use ref="textRef" inside the paragraph tag to create the template ref.
2
Create the template ref variable in script setup
In the <script setup> block, import ref from 'vue' and create a template ref variable named textRef initialized to null.
Vue
Hint
Use import { ref } from 'vue' and then const textRef = ref(null).
3
Add a button and a function to change text color
Add a button with the text "Change Color" inside the template. In the <script setup> block, create a function named changeColor that sets the paragraph's style color to 'blue' using the textRef template ref. Bind the button's click event to the changeColor function.
Vue
Hint
Define changeColor function and use textRef.value.style.color = 'blue'. Add a button with @click="changeColor".
4
Add accessibility and responsive styling
Add an aria-label attribute with value "Change text color" to the button for accessibility. Also, add a simple responsive style inside a <style scoped> block that sets the paragraph font size to 1.5rem on screens wider than 600px.
Vue
Hint
Add aria-label="Change text color" to the button. Use a media query @media (min-width: 600px) to set p font size to 1.5rem.
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?