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

Vue.js Custom Directives

Vue.js 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

// register a global custom directive v-focus Vue.directive('focus', { // when the bound element is inserted into the DOM. inserted: function (el) { // focus the element el.focus() } }) // create root instance new Vue({ el: '#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

// create root instance new Vue({ el: '#app', directives: { // register a local custom directive v-focus focus: { // definition of the directive inserted: function (el) { // focus the element el.focus() } } } })

script

Try it »


Hook

Hook Function

The directive definition function provides several hook functions (optional):

Hook Function Arguments

The hook function's arguments are:

- **name**: the directive name, excluding the `v-` prefix. - **value**: the binding value of the directive, for example: `v-my-directive="1 + 1"`, the value of value is `2`. - **oldValue**: the previous value bound to the directive, only available in the `update` and `componentUpdated` hooks. Available regardless of whether the value changed. - **expression**: the expression or variable name of the binding value. For example `v-my-directive="1 + 1"`, the value of expression is `"1 + 1"`. - **arg**: the argument passed to the directive. For example `v-my-directive:foo`, the value of arg is `"foo"`. - **modifiers**: an object containing modifiers. For example: `v-my-directive.foo.bar`, the value of the modifiers object is `{ foo: true, bar: true }`.

The following example demonstrates the use of these arguments:

Example

<div id="app" v-runoob:hello>

.

a

.

b

=

"

message

"

div

<

script

Vue.directive('runoob', { bind: function (el, binding, vnode) { var s = JSON.stringify el.innerHTML = 'name: ' + s(binding.name) + '

<

br

' + 'value: ' + s(binding.value) + '

<

br

' + 'expression: ' + s(binding.expression) + '

<

br

' + 'argument: ' + s(binding.arg) + '

<

br

' + 'modifiers: ' + s(binding.modifiers) + '

<

br

' + 'vnode keys: ' + Object.keys(vnode).join(', ') } }) new Vue({ el: '#app', data: { message: 'Runoob Tutorial!' } })

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 Tutorial!' }

"

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 »