KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🖖

Vue

Topic Hub & Articles

Vue Intro

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Get Started

10 min

Vue Directives

10 min

Vue Events

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Forms

10 min

Vue Components

10 min

Vue Props

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Methods

10 min

Vue Computed Properties

10 min

Vue Watchers

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Class and Style Bindings

10 min

Vue Conditional Rendering

10 min

Vue List Rendering

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Template Syntax

10 min

Vue Teleport

10 min

Vue Suspense

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Transitions

10 min

Vue Mixins

10 min

Vue Custom Directives

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Plugins

10 min

Vue Slots

10 min

Vue Provide / Inject

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Dynamic Components

10 min

Vue Async Components

10 min

Composition API Intro

10 min

Recap Quiz

Gated

Milestone Assessment

ref vs reactive

10 min

Lifecycle Hooks

10 min

Dependency Injection

10 min

Recap Quiz

Gated

Milestone Assessment

Computed and Watch

10 min

Template Refs

10 min

Composable Functions

10 min

Script Setup

10 min

Progress
0%

0 / 32 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
VueVue Tutorial
Lesson

Vue Custom Directives

10 min reading
Free Course

Vue Custom Directives: Direct DOM Manipulation and Hooks

Vue custom directives let you reuse low-level DOM behavior across elements when declarative bindings are not enough. Examples include focusing an input on mount, detecting clicks outside a dropdown, or attaching scroll observers.

In <script setup>, any camelCase variable starting with lowercase v (such as vFocus) registers automatically as a directive (v-focus). You can also register directives globally in main.js with app.directive().

flowchart LR
    Directive["v-focus in Template"] --> Hook["mounted(el, binding)"]
    Hook --> DOM["el.focus() Native DOM Call"]

Custom Directive Lifecycle Hooks

Each custom directive object can define hooks matching Vue's component lifecycle:

  • created(el, binding, vnode): Runs before the element's attributes or event listeners apply.
  • mounted(el, binding, vnode): Runs once the element is inserted into the DOM, making it safe to focus or measure.
  • updated(el, binding, vnode): Runs after the parent component and all its children update.
  • unmounted(el, binding): Runs when the element leaves the DOM so you can remove global event listeners.

Practical SFC Custom Directive Example

This component defines two local directives inside <script setup>: vFocus for auto-focusing inputs, and vHighlight for reading reactive values from binding.value.

<script setup>
import { ref } from 'vue';

const vFocus = {
  mounted(el) {
    el.focus();
  }
};

const vHighlight = {
  mounted(el, binding) {
    el.style.backgroundColor = binding.value || '#fef08a';
  },
  updated(el, binding) {
    el.style.backgroundColor = binding.value || '#fef08a';
  }
};

const highlightColor = ref('#bfdbfe');
</script>

<template>
  <div class="directive-demo">
    <label>
      Auto-focused Input:
      <input v-focus type="text" placeholder="Focused on mount" />
    </label>

    <p v-highlight="highlightColor">
      This paragraph uses a custom directive for background color.
    </p>

    <button @click="highlightColor = '#bbf7d0'">Switch to Green</button>
  </div>
</template>

Common Mistakes

  • Only Defining mounted Without updated: If your directive reads a reactive binding.value, forgetting the updated hook stops the DOM from updating when that value changes.
  • Using Custom Directives Instead of Components: Avoid custom directives for layout, state management, or rendering markup. Components and props are much easier to maintain for UI composition.
  • Forgetting Cleanup in unmounted: If mounted attaches window.addEventListener, always call window.removeEventListener in unmounted and store the handler on el or a WeakMap.

Frequently Asked Questions

What properties does the binding object contain?

binding includes value (the passed value), oldValue (previous value in updated), arg (such as 'foo' in v-my:foo), and modifiers (such as { prevent: true }).

How do I write a shorthand function directive?

If you only need the same logic for both mounted and updated, pass a single function instead of an object: const vHighlight = (el, binding) => { ... }.

Should I apply custom directives onto components?

Not recommended. In Vue 3, components can have multiple root nodes, so directives placed on component tags behave unpredictably.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

Related Developer Tools & Topic Hub

Vue Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format HTML templates
CSS Formatter
Clean and format stylesheets
All Vue Articles
Deep-dive guides, tips & discussions
PreviousVue Mixins
Continue JourneyTake Recap Quiz

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum