π Vue Template Refs β Directly Access DOM Elements and Components (2025 Guide)
π§² Introduction β What Are Template Refs in Vue?
Template refs in Vue.js allow you to directly reference DOM elements or child components inside your component logic. This is especially useful when you need fine-grained control over the DOM, such as focusing an input, measuring an element, or interacting with component methods.
π― In this guide, youβll learn:
- What Vue refs are and how to use them
- How to reference DOM nodes and child components
- How to use
ref
with the Composition API - Practical examples with step-by-step code
π What Is a Template Ref?
A template ref is a way to assign an identifier to an element or component in the template so you can access it in JavaScript.
<input ref="myInput" />
Then in your script:
this.$refs.myInput.focus();
π§± Example β Accessing DOM with ref
β Focusing an Input Field
<template>
<div>
<input ref="searchBox" placeholder="Type here..." />
<button @click="focusInput">Focus</button>
</div>
</template>
<script>
export default {
methods: {
focusInput() {
this.$refs.searchBox.focus();
}
}
}
</script>
π§ ref="searchBox"
makes the input accessible via this.$refs.searchBox
.
π Refs with Child Components
You can also use ref
to call methods or access data in child components.
β Child Component
<template><button @click="alertMessage">Say Hi</button></template>
<script>
export default {
methods: {
alertMessage() {
alert('Hello from child!');
}
}
}
</script>
β Parent Component
<template>
<ChildComp ref="childRef" />
<button @click="callChildMethod">Trigger Child</button>
</template>
<script>
import ChildComp from './ChildComp.vue';
export default {
components: { ChildComp },
methods: {
callChildMethod() {
this.$refs.childRef.alertMessage();
}
}
}
</script>
π§ Great for creating interactive dashboards and modular UI interactions.
𧬠Template Refs with Composition API
Use ref()
from Vue’s reactivity system along with onMounted()
.
<template>
<input ref="inputEl" placeholder="Enter something..." />
</template>
<script setup>
import { ref, onMounted } from 'vue';
const inputEl = ref(null);
onMounted(() => {
inputEl.value.focus();
});
</script>
π§ ref(null)
sets up a reactive reference. The actual DOM element is assigned after mount.
π§° Real-World Use Cases
Use Case | How Refs Help |
---|---|
Focusing form inputs | inputRef.focus() |
Accessing component methods | childRef.methodName() |
Getting DOM size/position | elementRef.getBoundingClientRect() |
Manual animations | Direct DOM manipulation for performance |
β οΈ Best Practices for Template Refs
Tip | Why It Matters |
---|---|
Use refs only when necessary | Avoid bypassing Vueβs reactivity |
Always check if ref is not null | DOM may not be ready in lifecycle hook |
Don’t mutate DOM via refs unnecessarily | Let Vue handle rendering where possible |
Prefer props and events for component comm | Refs are for edge cases, not communication |
π Summary β Recap & Next Steps
Template refs in Vue provide a powerful way to manipulate the DOM or interact with components directly. While Vue promotes declarative logic, refs serve well in cases where direct access is essential.
π Key Takeaways:
- Use
ref="name"
in templates to assign references - Access with
this.$refs.name
(Options API) orref()
(Composition API) - Ideal for DOM tasks like focus, scroll, or dimension measuring
- Use sparingly and wisely to maintain clean architecture
βοΈ Real-World Relevance:
Template refs are essential in modals, form handling, dynamic layouts, and custom UI widgets.
β FAQ Section
β What is a ref
in Vue?
β Itβs a way to reference a DOM element or component instance from within your component logic.
β How do I use ref
in Vue 3 Composition API?
β
Use const el = ref(null)
and access it after onMounted()
:
el.value.focus();
β Can I use ref
with child components?
β
Yes. You can access public methods of a child component using this.$refs.childName
.
β What’s the difference between ref()
and ref="..."
?
β
ref="..."
is for the template; ref()
is for reactive variables in JavaScript.
β Should I use refs for communication between components?
β
No. Prefer props
and $emit()
for parent-child communication. Use ref
only when necessary.
Share Now :