Chapter 70: Vue Directives

Vue Directives, one of the most iconic, most loved, and most instantly recognizable features that makes Vue feel like “HTML on steroids”.

Vue Directives are special attributes that start with v- (like v-if, v-for, v-model, v-bind, v-on, etc.). They are instructions that tell Vue to do something special to the element or component they are attached to.

Think of them like super-powers you attach directly to normal HTML tags:

  • Normal HTML: <div>Hello</div>
  • Vue with directive: <div v-if=”isLoggedIn”>Welcome back!</div>

Vue sees v-if, understands the logic, and automatically adds/removes the <div> from the DOM based on whether isLoggedIn is true or false.

1. Why Directives Feel Magical (The Big Picture)

Directives are what make Vue declarative instead of imperative.

Imperative (old-school jQuery way):

JavaScript

Declarative (Vue directive way):

HTML

Vue automatically handles showing/hiding — you just describe what should happen, not how to do it.

2. The Core Built-in Directives (What You Use Every Day)

Here’s the main list every Vue developer should know by heart (in rough order of daily usage):

Directive Shorthand What it does Most common real-world use-case Example (2026 style)
v-bind : Bind dynamic value to attribute / prop class, style, src, id, :to (router), :disabled :class=”{ active: isActive }”
v-on @ Listen to events @click, @submit, @keyup.enter, custom events @click=”increment”
v-model Two-way data binding on form inputs inputs, textarea, checkbox, select, custom inputs v-model=”form.email”
v-if / v-else-if / v-else Conditional rendering (remove/add from DOM) show/hide modals, loading states, auth gates v-if=”user.isLoggedIn”
v-show Conditional visibility (display: none) frequent toggles (tabs, accordions) v-show=”isLoading”
v-for Loop over arrays / objects lists, tables, cards, options in select v-for=”todo in todos” :key=”todo.id”
v-text Set textContent (safer than {{ }} in some cases) When you want to avoid mustache parsing issues v-text=”rawText”
v-html Render raw HTML (dangerous – XSS risk!) Only trusted content from server v-html=”trustedHtml”
v-once Render once, never update again Static content (prices, timestamps) <span v-once>{{ price }}</span>
v-pre Skip compilation — show as raw text Debugging or showing mustache syntax literally <span v-pre>{{ notCompiled }}</span>
v-cloak Hide element until Vue finishes compiling Prevent flash of unstyled content (FOUC) <div v-cloak>…</div> + CSS [v-cloak] { display: none }

3. Real, Practical Example – Login Form (using almost all core directives)

vue

Quick Summary Table – Core Directives You Use Every Day

Directive Shorthand Purpose Most common gotcha / tip
v-bind : Dynamic attributes / props Always use : for JS values
v-on @ Event listeners Modifiers: .prevent, .stop, .once
v-model Two-way binding .trim, .number, .lazy
v-if Conditional render (remove/add) Use <template v-if> for groups
v-show Conditional visibility (display: none) Cheaper than v-if for frequent toggles
v-for Loop Always :key – unique & stable
v-html Raw HTML (dangerous!) Only trusted content – XSS risk
v-once Render once, never update Good for static content
v-cloak Hide until Vue boots Prevent flash of mustache

Pro Tips from Real Projects (Hyderabad 2026)

  • Always use :key in v-for — Vue warns in console if missing
  • Prefer shorthand (:class, @click) — cleaner & faster to type
  • Use <template> with v-if / v-for when you don’t want extra wrapper <div>
  • Never put complex logic inside {{ }} — move to computed
  • For accessibility → prefer semantic HTML + proper aria-* attributes with :aria-*
  • Use v-cloak + CSS [v-cloak] { display: none } in production to prevent FOUC

Your mini practice:

  1. Build a small login form using:
    • v-model.trim + .lazy
    • v-if / v-else for validation messages
    • :disabled on submit button
    • @submit.prevent
    • v-once for copyright footer
  2. Add v-cloak to the form → see it hidden until Vue boots

Any directive still confusing? Want full examples for:

  • v-for + <TransitionGroup> animation?
  • Custom component with multiple v-model?
  • v-html vs v-text vs {{ }} security comparison?
  • All directives in one realistic dashboard card?

Just tell me — we’ll build the next clean, real-world template together 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *