Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Custom Directives

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

<

script

const 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

<

script

const 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):

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:

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 »