Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Built-in Directives

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

Visible

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

Visible

Not Visible

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
  • {{ item }}
  • 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
    {{ message }}
    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

    {{ message }}

    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 valueA and valueB all remain unchanged, this

    and all its children's updates will be skipped. In fact, even the virtual DOM vnode creation is skipped, because the cached subtree copy can be reused.

    |

    v-text

    • Usage:

    • Description: Updates the element's textContent, similar to interpolation {{ }}, but is one-way binding. Can be used to set the element's plain text content.

    Example

    Example

    data

    (

    )

    {

       

    return

    {

        message

    :

    'Hello, Vue!'

       

    }

    ;

    }

    v-html

    • Usage:
    • Description: Updates the element's innerHTML, used to output content containing HTML structure. Be mindful of security to avoid XSS attacks.

    Example

    Example

    data

    (

    )

    {

       

    return

    {

            htmlContent

    :

    'Vue.js is awesome!'

       

    }

    ;

    }

    v-model

    • Usage:
    • Description: Creates two-way data binding on form control elements, syncing the input field with the data property, supporting various form control types.

    Example

    Example

    data

    (

    )

    {

       

    return

    {

        message

    :

    ''

       

    }

    ;

    }

    v-show

    • Usage:
    • Description: Toggles the element's display and hiding based on the truthiness of an expression, using CSS's display property to control the element's visibility.

    Example

    Visible when true

    Example

    data

    (

    )

    {

       

    return

    {

        isVisible

    :

    true

       

    }

    ;

    }

    v-if / v-else / v-else-if

    • Usage:

      Visible

      Not Visible

    • Description: Conditionally renders an element based on the truthiness of an expression.v-else and v-else-if Is v-if 's supplementary directive, used for multi-condition branching.

    Example

    Visible

    Not Visible

    Example

    data

    (

    )

    {

       

    return

    {

            isVisible

    :

    true

       

    }

    ;

    }

    v-for

    • Usage:
    • {{ item }}
    • Description: Iterates over each element of an array or object, generating the corresponding number of elements, and supports specifying a key to improve rendering efficiency.

    Example

    • {{ item }}

    Example

    data

    (

    )

    {

       

    return

    {

            items

    :

    [

    'Apple'

    ,

    'Banana'

    ,

    'Cherry'

    ]

       

    }

    ;

    }

    v-on (@)

    • Usage:
    • Description: Binds an event listener to listen for DOM events, can be abbreviated as @. Supports modifiers and dynamic event names.

    Example

    Example

    methods

    :

    {

        handleClick

    (

    )

    {

        alert

    (

    'Button clicked'

    )

    ;

       

    }

    }

    v-bind ( : )

    • Usage:
    • Description: Dynamically binds HTML attributes, can be abbreviated as :. Used to dynamically set element attributes, such as src, href , etc.

    Example

    Vue Logo

    Example

    data

    (

    )

    {

       

    return

    {

        imageSrc

    :

    'https://vuejs.org/images/logo.png'

       

    }

    ;

    }

    v-slot

    • Usage:
    • Description: The syntax for named slots, providing a location to insert child component content, used for custom component content distribution.

    Example

       

       

    Example

    // BaseLayout component definition

    Vue.

    component

    (

    'BaseLayout'

    ,

    {

        template

    :

    `

       

    <
    
    div
    
    <
    
    header
    
    <

    slot name

    =

    "header"

    slot

    header

           

    <
    
    main
    
    <
    
    slot
    
    slot
    
    main
    
    <
    
    footer
    
    <

    slot name

    =

    "footer"

    slot

    footer

       

    div

        `

    }

    )

    ;

    v-pre

    • Usage:
      {{ message }}
    • Description: Skips the compilation of this element and all its children, used to display raw Mustache tags and prevent Vue from compiling the content.

    Example

    {{ message }}

    Example

    data

    (

    )

    {

       

    return

    {

        message

    :

    'Hello, Vue!'

       

    }

    ;

    }

    v-cloak

    • Usage: [v-cloak] { display: none; }
    • Description: Hides template content before the Vue instance is compiled, avoiding the display of uncompiled Mustache tags.

    Example

    {{ message }}

    Example

    [

    v-cloak

    ]

    {

    display

    :

    none

    ;

    }

    Example

    data

    (

    )

    {

       

    return

    {

        message

    :

    'Hello, Vue!'

       

    }

    ;

    }

    v-once

    • Usage:

      {{ message }}

    • Description: Renders the element and component only once, and does not update with subsequent data changes, used for static or infrequently updated content.

    Example

    {{ message }}

    Example

    data

    (

    )

    {

       

    return

    {

        message

    :

    'Hello, Vue!'

       

    }

    ;

    }