Following is the list of most commonly used directives in Vue.js:
- v-show
- v-if
- v-model
- v-else
- v-on
In Vue.js, directives are special tokens in the markup that tell the library to do something to a DOM element. Here are some commonly used directives in Vue.js:
- v-bind: It binds an attribute to an expression. For example,
v-bind:href="url"
binds thehref
attribute of an element to the value ofurl
in the Vue instance’s data. - v-model: It creates a two-way binding on form input elements or components. It automatically syncs the data between the input and the Vue instance.
- v-if / v-else-if / v-else: These directives are used for conditional rendering. They conditionally render a block based on the provided expression.
- v-for: It renders a list of items based on an array or object. It iterates over the given array or object and renders each item.
- v-on: It attaches event listeners to elements. For example,
v-on:click="handleClick"
listens for a click event on the element and calls thehandleClick
method. - v-show: It toggles the visibility of an element based on the truthiness of the expression. Unlike
v-if
, it does not remove the element from the DOM, it just toggles thedisplay
CSS property. - v-html: It renders HTML markup inside an element. It is important to be cautious when using
v-html
as it can lead to XSS vulnerabilities if used with user-generated content.
These are some of the most commonly used directives in Vue.js, but there are more available for specific use cases.