What are Directives in Vue and How to Implement Them

If you're starting with Vue.js, one of the first concepts you need to master is directives. These are special instructions that extend functionalities when inserted into HTML, enabling the creation of more dynamic and reactive interfaces simply. In Vue.js, directives are marked with the v- prefix and are responsible for implementing specific behaviors in elements.

With them, you can control element display, bind data reactively, handle events, and even create conditional interactions without directly modifying the page structure.

Now that you understand the meaning and relevance of directives, let's explore the native directives available in Vue.js and how to implement them in practice.

What Are the Main Directives in Vue.js?

Vue.js provides a range of built-in directives for performing common operations. Here are some of the most commonly used directives:

  • v-bind: Allows dynamic modification of attributes or properties of HTML elements.
  • v-model: Facilitates form creation by enabling two-way data binding. This ensures that information entered in input fields is automatically synchronized with the application's state.
  • v-on: Handles JavaScript events within Vue.js, linking user actions to program logic.
  • v-for: Used to dynamically display lists. It iterates over arrays or objects and automatically creates an element for each item.
  • v-if: Dynamically includes or excludes elements based on conditions. It manages rendering by inserting or removing elements.
  • v-show: Controls element visibility based on conditions. Unlike v-if, which adds or removes elements from the DOM, v-show only changes visibility via CSS (display: none).

Let's Explore in Practice

Binding Attributes (v-bind)

The v-bind directive is used to bind a value to an HTML tag, creating a connection between values. Its syntax is simple. Here's an example:

<template>
  <a v-bind:href="url">Visit our website</a>
</template>

<script setup>
import { ref } from "vue";

const url = ref("https://codigoaoponto.com/");
</script>

In the code above, we bind the url variable to the <a> tag, ensuring that its value is inserted into the href attribute. If the url variable changes, the href attribute will automatically update.

Two-Way Binding (v-model)

The v-model directive binds an application variable to an <input> element in the template. It works bidirectionally, meaning any change in the variable will reflect in the input, and vice versa. Example:

<template>
  <input v-model="name" type="text" />
  <div>Hello! My name is {{ name }}</div>
</template>

<script setup>
import { ref } from "vue";

const name = ref("Test");
</script>

Here, we associate the name variable with the <input> tag, ensuring its reactivity and enabling two-way data binding!

Event Handling (v-on)

The v-on directive handles events within Vue.js, typically used for actions like button clicks, form submissions, or element focus. Example:

<template>
  <input v-model="name" type="text" />
  <div>Hello! My name is {{ name }}</div>
  <button v-on:click="reset">Reset</button>
</template>

<script setup>
import { ref } from "vue";

const name = ref("Test");

function reset() {
  name.value = "";
}
</script>

When clicking the button, the reset function resets the name variable to an empty string.

Rendering Lists (v-for)

The v-for directive works similarly to JavaScript's for loop, iterating over lists, arrays, or objects and dynamically generating elements. Example:

<template>
  <ul>
    <li v-for="(name, index) in names" v-bind:key="index">
      {{ name }}
    </li>
  </ul>
</template>

<script setup>
const names = ["Código ao Ponto", "Eduardo", "Thiago", "Ricardo", "Cleiton"];
</script>

Here, we're creating a list of names based on an array. The v-bind:key attribute is essential when using v-for to provide a unique identifier for each list element, improving tracking and management.

Conditional Rendering (v-if and v-else)

The v-if and v-else directives dynamically include or exclude elements based on a condition. Example:

<template>
  <div>
    <input v-model="name" type="text" />
    <div v-if="name">Hello! My name is {{ name }}</div>
    <div v-else>Please enter your name.</div>
    <button v-on:click="reset">Reset</button>
  </div>
</template>

<script setup>
import { ref } from "vue";

const name = ref("");

function reset() {
  name.value = "";
}
</script>

Visibility Control (v-show)

The v-show directive controls an element's visibility without removing it from the DOM. Example:

<template>
  <div>
    <input v-model="name" type="text" />
    <div>Hello! My name is {{ name }}</div>
    <div v-show="name.length > 10">Your name is too long!</div>
  </div>
</template>

<script setup>
import { ref } from "vue";

const name = ref("");
</script>

Final Considerations

Now that you're familiar with Vue.js's main directives, including v-if, v-else, v-show, v-for, v-bind, and v-on, you can apply these concepts to build more dynamic and efficient applications!