CHAPTER 08
Beginner
Directives in Vue.js
Updated: May 18, 2026
5 min read
# CHAPTER 8
Directives in Vue.js
1. Chapter Introduction
Directives are special HTML attributes prefixed withv- that tell Vue to do something to a DOM element. They are the syntax bridge between your reactive data and the DOM. Vue provides 14 built-in directives covering every common DOM manipulation pattern.
2. Learning Objectives
- Use all major built-in Vue directives.
-
Understand the difference between
v-ifandv-show.
-
Build efficient lists with
v-forand:key.
- Create custom directives for reusable DOM behavior.
3. v-bind — Dynamic Attributes
vue
4. v-model — Two-Way Binding
vue
5. v-if, v-else-if, v-else
vue
6. v-if vs v-show
vue
7. v-for — List Rendering
vue
8. Custom Directives
vue
9. Other Useful Directives
vue
10. Common Mistakes
-
v-forwithout:key: Vue warns and cannot efficiently update the list. Always provide a unique, stable key (ideally an ID from your data).
-
Using
v-ifandv-foron the same element: Never do<li v-if="..." v-for="...">.v-forhas higher priority. Use<template v-for>outside with<li v-if>inside.
11. MCQs
Question 1
v-model.number does?
Question 2
What is the key difference between v-if and v-show?
Question 3
:key in v-for should be?
Question 4
Custom directive is accessed with?
Question 5
v-model.lazy updates on?
Question 6
v-for with range v-for="n in 5" produces?
Question 7
What does v-pre do?
Question 8
Best approach for v-if + v-for together?
Question 9
Custom directive mounted hook receives?
Question 10
v-model.trim does?
12. Interview Questions
-
Q: What is the difference between
v-ifandv-show? When do you choose each?
-
Q: Why should you never use
v-ifandv-foron the same element?
13. Summary
Vue's directive system provides a declarative interface for every common DOM behavior.v-bind and v-model handle data flow. v-if/v-show handle conditional display. v-for handles list rendering. Custom directives encapsulate DOM-specific logic into reusable plugins.