Explain it simply
Custom Directives and Plugins, in plain language
Only reach for a custom directive when you need to reuse low-level DOM behavior. If UI structure is involved, a component or composable is usually the better fit. Use a plugin's install(app) to bundle up app-wide services and directives.
Problem → Why → Solution
The problem — why this concept matters
Overcomplicating plain template logic by turning it into a directive
A common mistake
Not cleaning up a directive's event listener in the unmounted hook
Core concept — understand how it works
Learn how to build a custom directive for DOM behavior and a plugin for app-wide capabilities.
- props or event
- component and template
- rendered Vue UI
Work with the custom directive lifecycle
Core syntax or pattern
<script setup>
const vFocus = {
mounted(element) {
element.focus()
}
}
</script>
<template>
<label>
Search
<input v-focus placeholder="Type to search">
</label>
</template>Learn how to build a custom directive for DOM behavior and a plugin for app-wide capabilities.
Read the code step by step
- props or event
<script setup>Only reach for a custom directive when you need to reuse low-level DOM behavior. If UI structure is involved, a component or composable is usually the better fit. Use a plugin's install(app) to bundle up app-wide services and directives.
- component and template
</script>Learn how to build a custom directive for DOM behavior and a plugin for app-wide capabilities.
- rendered Vue UI
</template>Apply Custom Directives and Plugins in a small example and test normal, edge, and failure cases.
- props or event
- component and template
- rendered Vue UI
Success
Work with the custom directive lifecycle
Failure
Overcomplicating plain template logic by turning it into a directive
Let's break it down simply
Only reach for a custom directive when you need to reuse low-level DOM behavior. If UI structure is involved, a component or composable is usually the better fit. Use a plugin's install(app) to bundle up app-wide services and directives.
<script setup>
const vFocus = {
mounted(element) {
element.focus()
}
}
</script>
<template>
<label>
Search
<input v-focus placeholder="Type to search">
</label>
</template>Search input receives focus after mountTry it yourself
Write a v-click-outside custom directive that calls a callback when you click outside the element.
Custom Directives — Vue.js
Real-world usage
How to apply it in a real project
Apply Custom Directives and Plugins in a small example and test normal, edge, and failure cases.
Mini challenge
Build it yourself
Write a v-click-outside custom directive that calls a callback when you click outside the element.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<script setup>
const vFocus = {
mounted(element) {
element.focus()
}
}
</script>
<template>
<label>
Search
<input v-focus placeholder="Type to search">
</label>
</template>Expected: Work with the custom directive lifecycle
Show hint
Apply Custom Directives and Plugins in a small example and test normal, edge, and failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- props or event → component and template → rendered Vue UI
- Learn how to build a custom directive for DOM behavior and a plugin for app-wide capabilities.
- Overcomplicating plain template logic by turning it into a directive
- Apply Custom Directives and Plugins in a small example and test normal, edge, and failure cases.
Custom Directives — Vue.js