Thuta Learning
Vue.js
IntermediateWeb DevelopmentBeginner

Custom Directives and Plugins

Vue.jsLesson 1620 min

What you'll walk away with

  • Work with the custom directive lifecycle
  • Choose between a directive and a component
  • Register a plugin

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.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Work with the custom directive lifecycle

Core syntax or pattern

vue
<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

  1. 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.

  2. component and template</script>

    Learn how to build a custom directive for DOM behavior and a plugin for app-wide capabilities.

  3. rendered Vue UI</template>

    Apply Custom Directives and Plugins in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. 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.

vue
<script setup>
const vFocus = {
  mounted(element) {
    element.focus()
  }
}
</script>

<template>
  <label>
    Search
    <input v-focus placeholder="Type to search">
  </label>
</template>
You should see
Search input receives focus after mount

Try 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
vue
<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

What best describes the core idea of Custom Directives and Plugins?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Overcomplicating plain template logic by turning it into a directive
  • Not cleaning up a directive's event listener in the unmounted hook

Exercise

Write a v-click-outside custom directive that calls a callback when you click outside the element.

You'll know it worked when: Search input receives focus after mount

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Custom Directives and Plugins | Thuta Learning