In Vue.js, directives are special tokens in the markup that tell the library to do something to a DOM element. Vue provides a set of built-in directives like v-if, v-show, v-for, and v-model, but in many cases, you might need to add custom behavior that is not covered by the existing directives. Vue.js allows you to create custom directives to extend its functionality and attach custom behavior to DOM elements.
A directive is a special marker in the template (usually prefixed by v-
) that tells Vue.js to apply specific behavior or functionality to a DOM element. For instance, directives control DOM rendering (v-if
), handle events (v-on
), and perform data binding (v-bind
).
Custom directives allow you to introduce your own behavior when Vue binds an element to the DOM.
Vue.js makes it easy to create and register custom directives, either globally or locally.
A global directive can be used anywhere in your Vue.js application. You register a global directive by using the Vue.directive()
method.
Vue.directive('directive-name', {
// directive definition object
});
focus
that automatically sets focus to an input element when it’s inserted into the DOM.inserted
hook is called when the element is added to the DOM, and el.focus()
is used to focus the input element.When the page loads, the input field will automatically be focused, without the user needing to click on it.
Custom directives can also be registered locally inside a Vue component. This means the directive is only available within that component.
autofocus
directive is defined locally inside the component and performs the same task as the global focus
directive.Custom directives in Vue.js have several lifecycle hooks that allow you to hook into different phases of the directive’s existence. These hooks provide fine-grained control over how your directive interacts with the DOM.
Hover over me to see the directive in action.
Like Vue’s built-in directives, custom directives can accept values, modifiers, and arguments.
You can pass values to custom directives just as you would with Vue’s built-in ones.
v-color
directive takes a value ('red'
), and sets the background color of the input element to red.The input field will have a red background.
Directives can also take arguments (such as an event name) or modifiers (extra flags for additional behavior).
v-color:background
directive sets the background color of the button to the value 'orange'
.arg
property is used to handle the argument ('background'
in this case).Modifiers in Vue.js provide extra options for custom directives. For example, you can have a directive behave differently based on the modifier applied.
v-focus.once
directive ensures that the input element gets focused only once when it’s inserted into the DOM.Directives can be dynamically updated based on the data passed in or other factors.
Change my color
isActive
.update
hook ensures that the color changes whenever isActive
changes.The text color will toggle between green and red when isActive
changes.
unbind
hook to avoid memory leaks or unexpected behavior.Vue.js custom directives offer a flexible way to add DOM-specific behavior to your components. They are useful when you need to manipulate the DOM directly, integrate third-party libraries, or handle repetitive tasks. Happy Coding!❤️