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):
-
bind: called only once, when the directive is first bound to the element; you can use this hook to define an initialization action performed once at binding time. -
inserted: called when the bound element is inserted into its parent node (the parent node only needs to exist, it does not have to be in the document). -
update: called when the template containing the bound element is updated, regardless of whether the binding value changes. By comparing the binding values before and after the update, you can ignore unnecessary template updates (see below for detailed hook function parameters). -
componentUpdated: called when the template containing the bound element completes one update cycle. -
unbind: called only once, when the directive is unbound from the element.
Hook Function Arguments
The hook function's arguments are:
- el: the element the directive is bound to, which can be used to directly manipulate the DOM.
- binding: an object containing the following properties:
- name: the directive name, excluding
v-prefix. - value: the binding value of the directive, for example:
v-my-directive="1 + 1", the value of value is2. - oldValue: the previous value bound to the directive, only available in
updateandcomponentUpdatedthe 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 modifiers is{ foo: true, bar: true }.
- vnode: the virtual node generated by Vue compilation.
- oldVnode: the previous virtual node, only available in
updateandcomponentUpdatedthe hooks.
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 »