π§Ύ Vue Directives Reference β The Complete Guide with Examples (2025)
π§² Introduction β What Are Vue Directives?
Vue directives are special attributes that extend HTML with reactive behavior. They are prefixed with v-
and tell Vue how to manipulate the DOM. Whether you’re showing elements conditionally, binding attributes, or handling user events, directives are core to Vue’s reactivity system.
π― In this guide, youβll learn:
- The list of Vue built-in directives
- Syntax, purpose, and practical examples
- Custom directive basics
- Differences between key directives like
v-if
andv-show
π List of Vue Built-in Directives
Directive | Purpose |
---|---|
v-bind | Binds attribute or prop dynamically |
v-model | Two-way binding for form elements |
v-if / v-else-if / v-else | Conditional rendering |
v-show | Toggles element visibility using display: none |
v-for | Iterates over lists |
v-on | Listens for DOM events |
v-slot | Used for named and scoped slots |
v-pre | Skips compilation for this element |
v-cloak | Hides uncompiled template until Vue is ready |
v-once | Renders the element once; no reactivity |
v-memo (Vue 3.2+) | Skips unnecessary re-renders based on dependencies |
π Detailed Reference of Key Directives
π v-bind
Binds dynamic attributes or props to HTML elements.
<img v-bind:src="imageUrl" />
<!-- shorthand -->
<img :src="imageUrl" />
π v-model
Two-way bind form inputs.
<input v-model="username" />
For custom components:
<custom-input v-model="user.age" />
β v-if
, v-else-if
, v-else
Conditionally render elements.
<p v-if="score > 80">Excellent</p>
<p v-else-if="score > 50">Good</p>
<p v-else>Try Again</p>
ποΈ v-show
Toggles visibility with display: none
, but keeps element in DOM.
<p v-show="isVisible">I am visible</p>
π v-for
Renders a list of items.
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
Also supports destructuring:
<div v-for="(value, key, index) in object">
{{ index }}: {{ key }} = {{ value }}
</div>
π― v-on
Listens to events.
<button v-on:click="submitForm">Submit</button>
<!-- shorthand -->
<button @click="submitForm">Submit</button>
With modifiers:
<input @keyup.enter="sendMessage" />
π v-slot
Use inside components to pass named or scoped slots.
<BaseLayout>
<template v-slot:header>
<h1>Title</h1>
</template>
</BaseLayout>
Scoped slot:
<CustomList v-slot="slotProps">
<p>{{ slotProps.item.text }}</p>
</CustomList>
π v-pre
Skips compilation for performance or raw rendering.
<span v-pre>{{ willNotBeCompiled }}</span>
πΆοΈ v-cloak
Used with v-cloak
attribute and CSS to prevent FOUC.
<p v-cloak>Loading...</p>
Add style:
[v-cloak] { display: none; }
π v-once
Render the element only once; no future reactivity.
<h1 v-once>{{ initialTitle }}</h1>
π§ v-memo
(Vue 3.2+)
Avoids re-renders if reactive dependencies donβt change.
<div v-memo="[dependency1, dependency2]">
{{ expensiveComputation }}
</div>
π οΈ Custom Directives (Advanced)
You can define your own directives.
app.directive('focus', {
mounted(el) {
el.focus();
}
});
Usage:
<input v-focus />
π§ v-if
vs v-show
β Quick Comparison
Feature | v-if | v-show |
---|---|---|
DOM Rendering | Adds/removes DOM | Toggles display CSS |
Performance | Costly for frequent toggle | Cheap for toggling |
Initial Render | No render unless true | Always rendered |
π Summary β Recap & Next Steps
Vue directives are a core part of the Vue templating engine, enabling dynamic DOM updates through reactive bindings. Understanding them will make your components more concise, declarative, and powerful.
π Key Takeaways:
- Use
v-bind
,v-model
,v-on
, andv-for
regularly - Prefer
v-show
for frequent toggles;v-if
for conditional structure - Learn slot-related directives like
v-slot
- Create custom directives for reusable DOM logic
βοΈ Real-World Relevance:
Directives are used in forms, conditionals, modals, lists, tabs, animations, and everywhere in Vue apps.
β FAQ Section
β What is the difference between v-if
and v-show
?
β
v-if
adds/removes elements in the DOM. v-show
just toggles visibility using CSS.
β Can I bind multiple attributes with v-bind
?
β Yes. Use object syntax:
v-bind="{ id: someId, class: someClass }"
β What does v-cloak
do?
β It hides uncompiled template until Vue has initialized. Often used with a CSS rule.
β What is v-once
used for?
β It renders an element once and skips it in future updates to improve performance.
β How do I create a custom directive?
β
Use app.directive()
to define behavior for DOM elements, like setting focus or formatting input.
Share Now :