Vue 3 Custom Directives
In addition to the built-in core directives (v-model and v-show), Vue also allows you to register custom directives.
Below we register a global directive v-focus, whose function is to give the element focus when the page loads:
Example
<div id="app">
<p>When the page loads, the input element automatically gains focus:
p
<
input
v-focus
div
<
scriptconst app = Vue.createApp({}) // register a global custom directive v-focus app.directive('focus', { // when the bound element is mounted into the DOM... mounted(el) { // focus the element el.focus() } }) app.mount('#app')
script
Try it »
We can also use the directives option in an instance to register a local directive, so that the directive can only be used within this instance:
Example
<div id="app">
<p>When the page loads, the input element automatically gains focus:
p
<
input
v-focus
div
<
scriptconst app = { data() { return { } }, directives: { focus: { // directive definition mounted(el) { el.focus() } } } } Vue.createApp(app).mount('#app')
Try it »
Hook
Hook Function
The directive definition function provides several hook functions (optional):
-
created: Called before the bound element's attributes or event listeners are applied. -
beforeMount: Called when the directive is first bound to the element and before the parent component is mounted. -
mounted: Called after the parent component of the bound element is mounted. -
beforeUpdate: Called before updating the VNode that contains the component. -
updated: Called after the VNode of the containing component and its children's VNodes are updated. -
beforeUnmount: Called only once before the parent component of the bound element is unmounted. -
unmounted: Called only once when the directive is unbound from the element and the parent component has been unmounted.
Example
import
{
createApp
}
from
'
vue
'
const
app
=
createApp
(
{
}
)
//
Registration
app
.
directive
(
'
my-directive
'
,
{
//
A directive has a set of lifecycle hooks:
//
Called before the bound element's attribute or event listener is applied
created
(
)
{
}
,
//
Called before the bound element's parent component is mounted
beforeMount
(
)
{
}
,
//
Called when the bound element's parent component is mounted
mounted
(
)
{
}
,
//
Called before the VNode of the containing component is updated
beforeUpdate
(
)
{
}
,
//
Called after the VNode of the containing component and its children's VNodes are updated
updated
(
)
{
}
,
//
Called before the bound element's parent component is unmounted
beforeUnmount
(
)
{
}
,
//
Called when the bound element's parent component is unmounted
unmounted
(
)
{
}
}
)
//
Register (functional directive)
app
.
directive
(
'
my-directive
'
,
(
)
=>
{
//
This will be used as mounted and updated called
}
)
//
getter, returns the directive definition if registered
const
myDirective
=
app
.
directive
(
'
my-directive
'
)
Hook Function Arguments
The hook function's arguments are:
el
el The element the directive is bound to. This can be used to directly manipulate the DOM.
binding
binding An object that contains the following properties:
instance: The component instance using the directive.value: The value passed to the directive. For example, inv-my-directive="1 + 1", the value is2.oldValue: The previous value, only available inbeforeUpdateandupdated. Available whether or not the value has changed.arg: The argument passed to the directive (if any). For example, inv-my-directive:foo, arg is"foo".modifiers: An object containing modifiers (if any). For example, inv-my-directive.foo.bar, the modifiers object is{foo: true,bar: true}.dir: An object passed as a parameter when registering the directive. For example, in the following directive:
app.directive('focus', {
mounted(el) {
el.focus()
}
})
dir will be the following object:
{
mounted(el) {
el.focus()
}
}
vnode
A blueprint of the real DOM element received as the el argument.
** prevNode**
The previous virtual node, only available in the beforeUpdate and updated hooks.
The following example demonstrates the use of these arguments:
Example
<div id="app">
<div v-runoob=">{ name: 'Runoob', url: 'www.runoob.com' }
"
div
div
<
script
const app = Vue.createApp({}) app.directive('runoob', (el, binding, vnode) => { console.log(binding.value.name) // => "Runoob" console.log(binding.value.url) // => "www.runoob.com" var s = JSON.stringify el.innerHTML = s(binding.value) }) app.mount('#app')
script
Try it »
Sometimes we don't need the other hook functions, so we can use a shorthand for the function, as in the following format:
Vue.directive('runoob', function (el, binding) {
// Set the background color of the command
el.style.backgroundColor = binding.value.color
})
The directive function can accept any valid JavaScript expression; the following example passes a JavaScript object:
Example
<div id="app">
<div v-runoob=">{ color: 'green', text: 'Runoob!' }
"
div
div
<
script
Vue.directive('runoob', function (el, binding) { // shorthand to set text and background color el.innerHTML = binding.value.text el.style.backgroundColor = binding.value.color }) new Vue({ el: '#app' })
script
Try it »