Vue 3 Built-in Directives
Vue.js has several built-in directives for adding specific reactive behavior or manipulating the DOM in templates.
The following are commonly used built-in directives in Vue.js:
| Instruction | Usage Examples | Description |
|---|---|---|
| v-text | |
Updates the element's textContent, similar to interpolation {{ }}, but is one-way binding. |
| v-html | |
Updates the element's innerHTML, used to output HTML. |
| v-model | |
Creates two-way data binding on form control elements, syncing the input field with the data property. |
| v-show | |
Toggles the element's display and hiding based on the truthiness of an expression, using CSS's display attribute. |
| v-if | |
Conditionally renders an element based on the truthiness of an expression; when the condition is false, the element is removed from the DOM. |
| v-else | |
v-if 's supplementary directive, used to show fallback content when the condition is false. |
| v-else-if | |
Used in sequence after v-if and v-else , for multi-condition branching. |
| v-for | |
Iterates over each element of an array or object, generating the corresponding number of elements. |
| v-on | |
Binds an event listener to listen for DOM events, can be abbreviated as @. |
| v-bind | |
Dynamically binds HTML attributes, can be abbreviated as :. |
| v-slot | |
The syntax for named slots, providing a location to insert child component content. |
| v-pre | |
Skips the compilation of this element and all its children, used to display raw Mustache tags. |
| v-cloak | [v-cloak] { display: none; } |
Hides template content before the Vue instance is compiled, avoiding the display of uncompiled Mustache tags. |
| v-once | |
Renders the element and component only once, and does not update with subsequent data changes. |
| v-memo |
|
... |
Caches a subtree of the template.
When the component re-renders, if data ( ) { return { message : 'Hello, Vue!' } ; } data ( ) { return { htmlContent : 'Vue.js is awesome!' } ; } data ( ) { return { message : '' } ; } Visible when true data ( ) { return { isVisible : true } ; } Visible Not Visible Visible Not Visible data ( ) { return { isVisible : true } ; } {{ item }} data ( ) { return { items : [ 'Apple' , 'Banana' , 'Cherry' ] } ; } methods : { handleClick ( ) { alert ( 'Button clicked' ) ; } } data ( ) { return { imageSrc : 'https://vuejs.org/images/logo.png' } ; } Header Content Footer Content // BaseLayout component definition Vue. component ( 'BaseLayout' , { template : ` slot name = "header"
slot
header slot name = "footer"
slot
footer
div ` } ) ; {{ message }} data ( ) { return { message : 'Hello, Vue!' } ; } {{ message }} [ v-cloak ] { display : none ; } data ( ) { return { message : 'Hello, Vue!' } ; } {{ message }} {{ message }} data ( ) { return { message : 'Hello, Vue!' } ; }valueA and valueB all remain unchanged, this v-text
textContent, similar to interpolation {{ }}, but is one-way binding. Can be used to set the element's plain text content.Example
Example
v-html
innerHTML, used to output content containing HTML structure. Be mindful of security to avoid XSS attacks.Example
Example
v-model
Example
Example
v-show
display property to control the element's visibility.Example
Example
v-if / v-else / v-else-if
v-else and v-else-if Is v-if 's supplementary directive, used for multi-condition branching.Example
Example
v-for
Example
Example
v-on (@)
@. Supports modifiers and dynamic event names.Example
Example
v-bind ( : )
:. Used to dynamically set element attributes, such as src, href , etc.Example
Example
v-slot
Header
Example
Example
<
div
<
header
<
<
main
<
slot
slot
main
<
footer
<
v-pre
Example
Example
v-cloak
[v-cloak] { display: none; }Example
Example
Example
v-once
Example
Example