Chapter 3: Vue Directives

Vue Directives are exactly that: special HTML attributes that start with v- (like v-if, v-for, v-model). Vue sees them and takes control — it watches your data and updates the DOM reactively.

This is declarative programming: you describe what should happen, not how to do it step-by-step.

In Vue 3 (2026 standard), directives work the same in Options API and Composition API — they’re part of the template syntax.

General Syntax of a Directive

HTML
  • v-directive → name (e.g. v-if, v-on)
  • :argument → optional (e.g. :class, :click)
  • .modifier → optional (e.g. .prevent, .trim)
  • “expression” → JavaScript expression (most cases — except a few like v-for)

Shorthands make life easier:

  • v-bind:xxx → :xxx
  • v-on:xxx → @xxx
  • v-slot:xxx → #xxx

Now let’s go through all the important built-in directives one by one, with real examples. I’ll use modern <script setup> style.

1. v-bind (:) — Dynamic attributes & props

Binds an attribute or prop to a JS value.

vue

Tip: Use :class and :style a lot — they’re super powerful.

2. v-on (@) — Event handling

Listens to events.

vue

Popular modifiers: .prevent, .stop, .once, .self, .passive, .capture, .enter, .esc, .tab, .left, etc.

3. v-model — Two-way data binding (forms magic)

Syncs input with data.

vue

v-model on components → custom inputs become easy (more later).

4. v-if / v-else-if / v-else — Conditional rendering (removes from DOM)

vue

Note: v-if is lazy — content isn’t even compiled/rendered until true. Good for performance.

5. v-show — Conditional visibility (stays in DOM, just display: none)

vue

v-show vs v-if:

  • Use v-show when toggling often (cheaper, keeps state)
  • Use v-if when condition rarely changes or for initial load performance

6. v-for — List rendering (the most used after v-bind/v-on)

vue

Rule: Always add :key (unique identifier) — helps Vue track items efficiently when list changes.

7. v-text & v-html — Text & raw HTML

vue

Never use v-html with user input unless sanitized!

8. Others worth knowing (less common but useful)

  • v-once → render once, never update again <span v-once>{{ willNeverChange }}</span>
  • v-memo (Vue 3.2+) → memoize subtree if deps unchanged <div v-memo=”[price, currency]”>Expensive block</div>
  • v-pre → skip compilation (show raw {{}}) <code v-pre>{{ raw mustache }}</code>
  • v-cloak → hide mustache flash in no-build setups CSS: [v-cloak] { display: none }
  • v-slot (#) → for scoped slots (we’ll cover in components lesson)

Quick Summary Table

Directive Purpose Shorthand Common Use Case
v-bind Bind attr/prop : :class, :style, :src, props
v-on Event listener @ @click, @submit.prevent
v-model Two-way form binding Inputs, checkboxes, custom comp
v-if / v-else Conditional render (remove/add) Auth checks, tabs
v-show Conditional visibility Frequent toggles ( accordions )
v-for Loop over list/object Lists, tables, options
v-html Inject raw HTML Trusted markdown/server content
v-text Set textContent Plain text output
v-once Static render once Prices, timestamps
v-memo Conditional memoization Performance in large lists

Pro Tip from 2026

In modern Vue 3 projects (Vite + <script setup>), you use directives a lot in templates — but heavy logic moves to computed, watch, composables.

Practice: Build a small app with all these — a filtered todo list with add/remove, search input (v-model), status toggle (v-show), role-based UI (v-if), etc.

Any directive confusing? Want a full mini-project example combining 5–6 of them? Or how to make a custom directive (like v-focus, v-click-outside)?

Just say — we’ll dive deeper 🚀

Happy Vue-directive-ing from Hyderabad! 🌶️

You may also like...

Leave a Reply

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