Vue Template Refs
Vue Template Refs are a powerful feature that enables you to directly access and manipulate DOM elements in a Vue.js application. By using the ref attribute and the $refs object, you can achieve dynamic and interactive user interfaces without relying on traditional DOM manipulation methods. In this article, we'll explore four advanced examples that showcase the versatility and creative potential of Vue Template Refs.
1. Interactive Form Validation
Objective: Leverage Vue Template Refs to build a real-time form validation system that provides instant feedback to users as they fill out a form.
Implementation
<!-- InteractiveFormValidation.vue -->
<template>
<div>
<h2>Interactive Form Validation</h2>
<form @submit.prevent="submitForm">
<label for="username">Username:</label>
<input type="text" ref="usernameInput" @input="validateUsername">
<p v-if="usernameError" class="error">{{ usernameError }}</p>
<label for="password">Password:</label>
<input type="password" ref="passwordInput" @input="validatePassword">
<p v-if="passwordError" class="error">{{ passwordError }}</p>
<button type="submit" :disabled="hasErrors">Submit</button>
</form>
</div>
</template>
<script>
export default {
data() {
return {
usernameError: null,
passwordError: null,
};
},
methods: {
validateUsername() {
// Validation logic for username
// Update this.usernameError accordingly
},
validatePassword() {
// Validation logic for password
// Update this.passwordError accordingly
},
submitForm() {
// Form submission logic
// Access input values using this.$refs.usernameInput.value and this.$refs.passwordInput.value
},
get hasErrors() {
return this.usernameError || this.passwordError;
},
},
};
</script>
<style scoped>
.error {
color: red;
}
</style>
This example demonstrates how Vue Template Refs can be used to create a dynamic form validation system. The validateUsername and validatePassword methods are triggered on input, updating the error messages dynamically. The form submission is controlled based on whether there are any validation errors.
2. Draggable Elements with Transition Effects
Objective: Implement a feature where users can drag and rearrange elements within a list with smooth transition effects.
Implementation
<!-- DraggableList.vue -->
<template>
<div>
<h2>Draggable List</h2>
<ul>
<li v-for="(item, index) in list" :key="index" ref="draggableItem" @mousedown="startDrag(index)">
{{ item }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
list: ['Item 1', 'Item 2', 'Item 3', 'Item 4'],
draggingIndex: null,
};
},
methods: {
startDrag(index) {
this.draggingIndex = index;
document.addEventListener('mousemove', this.handleDrag);
document.addEventListener('mouseup', this.stopDrag);
},
handleDrag(event) {
// Dragging logic to update element positions
},
stopDrag() {
this.draggingIndex = null;
document.removeEventListener('mousemove', this.handleDrag);
document.removeEventListener('mouseup', this.stopDrag);
},
},
};
</script>
<style scoped>
ul {
list-style-type: none;
padding: 0;
}
li {
cursor: grab;
margin: 5px;
padding: 10px;
border: 1px solid #ccc;
transition: transform 0.3s ease-in-out;
}
li:active {
cursor: grabbing;
transform: scale(1.1);
}
</style>

Join the conversation! Your thoughts help the community grow.