Vue.js directives are special tokens in the template that tell the library to do something to a DOM element. They come with a special v- prefix to indicate that they are special attributes provided by Vue.js. Directives are an essential feature of Vue.js, enabling dynamic and reactive data binding, conditional rendering, list rendering, event handling, and more. In this topic, we will cover Vue.js directives in detail, from basic to advanced concepts, with examples and explanations.
Directives are special tokens in Vue.js that attach special behavior to the DOM. They are prefixed with v-
to indicate they are special attributes provided by Vue.js. Directives bind data to the DOM, listen to events, or modify the DOM in response to data changes. Understanding directives is key to leveraging the full power of Vue.js.
The v-bind
directive dynamically binds one or more attributes, or a component prop, to an expression.
v-bind:href="url"
binds the href
attribute to the url
data property.https://vuejs.org
.The v-model
directive creates a two-way binding on form input elements.
{{ message }}
v-model="message"
binds the input value to the message
data property.
Hello Vue!
The v-on
directive is used to listen to DOM events.
v-on:click="greet"
listens for the click
event and calls the greet
method.
These directives control the conditional rendering of elements.
Type A
Type B
Other Type
v-if
, v-else-if
, and v-else
control which paragraph is rendered based on the type
data property.
Type A
The v-show
directive toggles the visibility of an element without removing it from the DOM.
I am visible
v-show="isVisible"
controls the visibility of the paragraph.
I am visible
The v-for
directive is used to render lists of items by iterating over arrays or objects.
- {{ item.text }}
v-for="item in items"
iterates over the items
array.:key="item.id"
is used to track each item’s identity.
- Learn JavaScript
- Learn Vue
- Build something awesome
The v-on
directive is used to listen to DOM events.
v-on:click="greet"
listens for the click
event and calls the greet
method.
Event modifiers are used to handle common event-related tasks like stopping propagation and preventing default behavior.
v-on:click.stop="greet"
stops the click event from propagating.v-on:submit.prevent="onSubmit"
prevents the form from submitting.The v-model
directive creates a two-way binding on form input elements.
{{ message }}
v-model="message"
binds the input value to the message
data property.
Hello Vue!
Modifiers allow fine-tuning of v-model
behavior.
{{ message }}
v-model.trim="message"
trims the input value.The v-bind
directive dynamically binds one or more attributes, or a component prop, to an expression.
v-bind:href="url"
binds the href
attribute to the url
data property.https://vuejs.org
.The v-bind:class
directive dynamically binds one or more classes to an element.
Hello Vue!
v-bind:class="{ active: isActive }"
binds the active
class to the isActive
data property.
Hello Vue!
The v-bind:style
directive dynamically binds one or more inline styles to an element.
Hello Vue!
v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"
binds the color
and fontSize
styles to the respective data properties.
Hello Vue!
Custom directives in Vue.js Directives can be registered globally to extend Vue’s functionality.
Highlight me!
Vue.directive('highlight', { ... })
registers a global custom directive named highlight
.el.style.backgroundColor = binding.value
sets the element’s background color.
Highlight me!
Custom directives can also be registered locally within a Vue instance.
Highlight me!
directives: { highlight: { ... } }
registers a local custom directive named highlight
.
Highlight me!
The v-pre
directive skips compilation for this element and all its children.
{{ raw }}
v-pre
tells Vue to leave the {{ raw }}
interpolation uncompiled.
{{ raw }}
The v-cloak
directive keeps the element cloaked until the Vue instance finishes compiling.
{{ message }}
v-cloak
hides the element until Vue’s compilation is complete.
Hello Vue!
The v-once
directive renders the element and component once and skips future updates.
{{ message }}
v-once
renders the message once and does not update it.
This will not change
In this topic, we explored Vue.js directives in-depth, from basic to advanced concepts. Vue.js directives are powerful tools for building dynamic and reactive applications. They allow you to bind data, handle events, conditionally render elements, iterate over lists, and even create custom behaviors. By mastering these directives, you can harness the full potential of Vue.js to build robust and efficient web applications.Happy coding !❤️