Vue 3 app.directive() Function Explained
app.directive() is a very powerful function that allows developers to define custom global directives.
A directive is a special feature provided by Vue for directly manipulating DOM elements. Through app.directive(), you can define your own directives and use them throughout the application.
1. What is a Directive?
A directive is a special attribute in Vue that starts with v- . Vue has some built-in commonly used directives, such as v-if, v-for, v-bind and so on. These directives help us manipulate DOM elements more conveniently.
For example,v-if directive can decide whether to render an element based on the value of an expression:
Example
Display content
When isVisible is true , thediv element is rendered; otherwise, thediv element is not rendered.
2. Custom Directives
Besides built-in directives, Vue also allows us to define our own directives. Custom directives can be used to handle specific DOM operations, such as auto-focus, scroll listening, etc.
In Vue 3, we can register a global directive via app.directive() function.
3. Basic Usage of app.directive()
app.directive() The function accepts two parameters:
- Directive name: Defines the directive's name; note not to include the
v-prefix. - Directive object: An object containing the directive's hook functions, used to define the directive's behavior.
3.1 Register a Simple Directive
Suppose we need to define a directive that auto-focuses an element when it is inserted into the DOM. We can implement it like this:
Example
import
{
createApp
}
from
'vue'
;
const
app
=
createApp
(
{
}
)
;
app.
directive
(
'focus'
,
{
mounted
(
el
)
{
el.
focus
(
)
;
}
}
)
;
app.
mount
(
'#app'
)
;
In this example, we defined a directive named focus . When the element is inserted into the DOM,mounted hook function is called,el.focus() makes the element automatically gain focus.
3.2 Using Custom Directives
When using custom directives in a template, you need to add the v- prefix:
Example
When the page loads, theinput element automatically gains focus.
4. Directive Hook Functions
Custom directives can contain multiple hook functions, used to perform specific operations at different lifecycle stages. Common hook functions include:
- mounted: Called when the element the directive is attached to is inserted into the DOM.
- updated: Called when the component the directive is attached to updates.
- unmounted: Called when the element the directive is attached to is removed from the DOM.
4.1 Example: Listening to Scroll Events
Suppose we need to perform some action when the user scrolls the page; we can define a scroll directive:
Example
app.
directive
(
'scroll'
,
{
mounted
(
el
,
binding
)
{
const
handler
=
binding.
value
;
el.
addEventListener
(
'scroll'
,
handler
)
;
}
,
unmounted
(
el
,
binding
)
{
const
handler
=
binding.
value
;
el.
removeEventListener
(
'scroll'
,
handler
)
;
}
}
)
;
In this example,mounted hook function adds a scroll event listener,unmounted hook function removes the listener.
4.2 Using the Scroll Directive
Example
Scrolling content...
Define onScroll method in the component:
Example
methods
:
{
onScroll
(
event
)
{
console.
log
(
'User is scrolling'
,
event
)
;
}
}
When the user scrolls the div element,onScroll method is called.
5. Directive Arguments and Modifiers
Custom directives can accept arguments and modifiers. The argument is the part after the colon following the directive name, and the modifier is the part after the dot following the directive name.
5.1 Example: Using Arguments and Modifiers
Suppose we need to define a color directive that sets the element's background color based on the argument:
Example
app.
directive
(
'color'
,
{
mounted
(
el
,
binding
)
{
const
color
=
binding.
arg
||
'red'
;
const
modifiers
=
binding.
modifiers
;
if
(
modifiers.
bold
)
{
el.
style
.
fontWeight
=
'bold'
;
}
el.
style
.
backgroundColor
=
color
;
}
}
)
;
Used in the template:
Example
Background color is blue, font is bold
In this example,blue is the directive's argument,bold is the modifier.
Vue 3 Global API