#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read
Question 15 of 37easy

What are Vue Directives?

Built-in and custom directives.

What You'll Learn

  • Built-in directives
  • Custom directives
  • Directive modifiers

Built-in Directives

code.txtVUE
<template>
  <!-- v-if / v-else-if / v-else -->
  <p v-if="show">Visible</p>
  <p v-else>Hidden</p>

  <!-- v-show (toggle display) -->
  <p v-show="show">Toggle visibility</p>

  <!-- v-for -->
  <li v-for="item in items" :key="item.id">
    {{ item.name }}
  </li>

  <!-- v-bind (shorthand :) -->
  <img :src="imageUrl" :alt="imageAlt">

  <!-- v-on (shorthand @) -->
  <button @click="handleClick">Click</button>

  <!-- v-model -->
  <input v-model="text">

  <!-- v-html -->
  <div v-html="htmlContent"></div>
</template>

Event Modifiers

code.txtVUE
<template>
  <form @submit.prevent="onSubmit">
  <button @click.stop="onClick">
  <input @keyup.enter="onEnter">
  <button @click.once="onClickOnce">
</template>

Custom Directive

code.jsJavaScript
// Focus directive
const vFocus = {
  mounted: (el) => el.focus()
}

// Usage: <input v-focus>