# Directives

# v-text

# v-html

  • Expects: string

  • Details:

    Updates the element's innerHTML (opens new window). Note that the contents are inserted as plain HTML - they will not be compiled as Vue templates. If you find yourself trying to compose templates using v-html, try to rethink the solution by using components instead.

    WARNING

    Dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to XSS attacks (opens new window). Only use v-html on trusted content and never on user-provided content.

    In single-file components, scoped styles will not apply to content inside v-html, because that HTML is not processed by Vue's template compiler. If you want to target v-html content with scoped CSS, you can instead use CSS modules (opens new window) or an additional, global <style> element with a manual scoping strategy such as BEM.

  • Example:

    <div v-html="'<h1>Hello World</h1>'"></div>
    
    1
  • See also: Data Binding Syntax - Interpolations

# v-show

  • Expects: any

  • Usage:

    Toggles the element's display CSS property based on the truthy-ness of the expression value.

    This directive triggers transitions when its condition changes.

  • See also: Conditional Rendering - v-show

# v-if

  • Expects: any

  • Usage:

    Conditionally render the element based on the truthy-ness of the expression value. The element and its contained directives / components are destroyed and re-constructed during toggles. If the element is a <template> element, its content will be extracted as the conditional block.

    This directive triggers transitions when its condition changes.

    When used together, v-if has a higher priority than v-for. We don't recommend using these two directives together on one element — see the list rendering guide for details.

  • See also: Conditional Rendering - v-if

# v-else

  • Does not expect expression

  • Restriction: previous sibling element must have v-if or v-else-if.

  • Usage:

    Denote the "else block" for v-if or a v-if/v-else-if chain.

    <div v-if="Math.random() > 0.5">
      Now you see me
    </div>
    <div v-else>
      Now you don't
    </div>
    
    1
    2
    3
    4
    5
    6
  • See also: Conditional Rendering - v-else

# v-else-if

  • Expects: any

  • Restriction: previous sibling element must have v-if or v-else-if.

  • Usage:

    Denote the "else if block" for v-if. Can be chained.

    <div v-if="type === 'A'">
      A
    </div>
    <div v-else-if="type === 'B'">
      B
    </div>
    <div v-else-if="type === 'C'">
      C
    </div>
    <div v-else>
      Not A/B/C
    </div>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
  • See also: Conditional Rendering - v-else-if

# v-for

  • Expects: Array | Object | number | string | Iterable

  • Usage:

    Render the element or template block multiple times based on the source data. The directive's value must use the special syntax alias in expression to provide an alias for the current element being iterated on:

    <div v-for="item in items">
      {{ item.text }}
    </div>
    
    1
    2
    3

    Alternatively, you can also specify an alias for the index (or the key if used on an Object):

    <div v-for="(item, index) in items"></div>
    <div v-for="(value, key) in object"></div>
    <div v-for="(value, name, index) in object"></div>
    
    1
    2
    3

    The default behavior of v-for will try to patch the elements in-place without moving them. To force it to reorder elements, you should provide an ordering hint with the key special attribute:

    <div v-for="item in items" :key="item.id">
      {{ item.text }}
    </div>
    
    1
    2
    3

    v-for can also work on values that implement the Iterable Protocol (opens new window), including native Map and Set.

    The detailed usage for v-for is explained in the guide section linked below.

  • See also:

# v-on

  • Shorthand: @

  • Expects: Function | Inline Statement | Object

  • Argument: event

  • Modifiers:

    • .stop - call event.stopPropagation().
    • .prevent - call event.preventDefault().
    • .capture - add event listener in capture mode.
    • .self - only trigger handler if event was dispatched from this element.
    • .{keyAlias} - only trigger handler on certain keys.
    • .once - trigger handler at most once.
    • .left - only trigger handler for left button mouse events.
    • .right - only trigger handler for right button mouse events.
    • .middle - only trigger handler for middle button mouse events.
    • .passive - attaches a DOM event with { passive: true }.
  • Usage:

    Attaches an event listener to the element. The event type is denoted by the argument. The expression can be a method name, an inline statement, or omitted if there are modifiers present.

    When used on a normal element, it listens to native DOM events (opens new window) only. When used on a custom element component, it listens to custom events emitted on that child component.

    When listening to native DOM events, the method receives the native event as the only argument. If using inline statement, the statement has access to the special $event property: v-on:click="handle('ok', $event)".

    v-on also supports binding to an object of event/listener pairs without an argument. Note when using the object syntax, it does not support any modifiers.

  • Example:

    <!-- method handler -->
    <button v-on:click="doThis"></button>
    
    <!-- dynamic event -->
    <button v-on:[event]="doThis"></button>
    
    <!-- inline statement -->
    <button v-on:click="doThat('hello', $event)"></button>
    
    <!-- shorthand -->
    <button @click="doThis"></button>
    
    <!-- shorthand dynamic event -->
    <button @[event]="doThis"></button>
    
    <!-- stop propagation -->
    <button @click.stop="doThis"></button>
    
    <!-- prevent default -->
    <button @click.prevent="doThis"></button>
    
    <!-- prevent default without expression -->
    <form @submit.prevent></form>
    
    <!-- chain modifiers -->
    <button @click.stop.prevent="doThis"></button>
    
    <!-- key modifier using keyAlias -->
    <input @keyup.enter="onEnter" />
    
    <!-- the click event will be triggered at most once -->
    <button v-on:click.once="doThis"></button>
    
    <!-- object syntax -->
    <button v-on="{ mousedown: doThis, mouseup: doThat }"></button>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

    Listening to custom events on a child component (the handler is called when "my-event" is emitted on the child):

    <my-component @my-event="handleThis"></my-component>
    
    <!-- inline statement -->
    <my-component @my-event="handleThis(123, $event)"></my-component>
    
    1
    2
    3
    4
  • See also:

# v-bind

  • Shorthand: : or . (when using .prop modifier)

  • Expects: any (with argument) | Object (without argument)

  • Argument: attrOrProp (optional)

  • Modifiers:

    • .camel - transform the kebab-case attribute name into camelCase.
    • .prop - force a binding to be set as a DOM property. 3.2+
    • .attr - force a binding to be set as a DOM attribute. 3.2+
  • Usage:

    Dynamically bind one or more attributes, or a component prop to an expression.

    When used to bind the class or style attribute, it supports additional value types such as Array or Objects. See linked guide section below for more details.

    When used for prop binding, the prop must be properly declared in the child component.

    When used without an argument, can be used to bind an object containing attribute name-value pairs. Note in this mode class and style does not support Array or Objects.

  • Example:

    <!-- bind an attribute -->
    <img v-bind:src="imageSrc" />
    
    <!-- dynamic attribute name -->
    <button v-bind:[key]="value"></button>
    
    <!-- shorthand -->
    <img :src="imageSrc" />
    
    <!-- shorthand dynamic attribute name -->
    <button :[key]="value"></button>
    
    <!-- with inline string concatenation -->
    <img :src="'/path/to/images/' + fileName" />
    
    <!-- class binding -->
    <div :class="{ red: isRed }"></div>
    <div :class="[classA, classB]"></div>
    <div :class="[classA, { classB: isB, classC: isC }]"></div>
    
    <!-- style binding -->
    <div :style="{ fontSize: size + 'px' }"></div>
    <div :style="[styleObjectA, styleObjectB]"></div>
    
    <!-- binding an object of attributes -->
    <div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>
    
    <!-- prop binding. "prop" must be declared in my-component. -->
    <my-component :prop="someThing"></my-component>
    
    <!-- pass down parent props in common with a child component -->
    <child-component v-bind="$props"></child-component>
    
    <!-- XLink -->
    <svg><a :xlink:special="foo"></a></svg>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

    When setting a binding on an element, Vue by default checks whether the element has the key defined as a property using an in operator check. If the property is defined, Vue will set the value as a DOM property instead of an attribute. This should work in most cases, but you can override this behavior by explicitly using .prop or .attr modifiers. This is sometimes necessary, especially when working with custom elements.

    The .prop modifier also has a dedicated shorthand, .:

    <div :someProperty.prop="someObject"></div>
    
    <!-- equivalent to -->
    <div .someProperty="someObject"></div>
    
    1
    2
    3
    4

    The .camel modifier allows camelizing a v-bind attribute name when using in-DOM templates, e.g. the SVG viewBox attribute:

    <svg :view-box.camel="viewBox"></svg>
    
    1

    .camel is not needed if you are using string templates, or compiling with vue-loader/vueify.

  • See also:

# v-model

  • Expects: varies based on value of form inputs element or output of components

  • Limited to:

    • <input>
    • <select>
    • <textarea>
    • components
  • Modifiers:

    • .lazy - listen to change events instead of input
    • .number - cast valid input string to numbers
    • .trim - trim input
  • Usage:

    Create a two-way binding on a form input element or a component. For detailed usage and other notes, see the Guide section linked below.

  • See also:

# v-slot

  • Shorthand: #

  • Expects: JavaScript expression that is valid in a function argument position (supports destructuring in supported environments). Optional - only needed if expecting props to be passed to the slot.

  • Argument: slot name (optional, defaults to default)

  • Limited to:

    • <template>
    • components (for a lone default slot with props)
  • Usage:

    Denote named slots or slots that expect to receive props.

  • Example:

    <!-- Named slots -->
    <base-layout>
      <template v-slot:header>
        Header content
      </template>
    
      <template v-slot:default>
        Default slot content
      </template>
    
      <template v-slot:footer>
        Footer content
      </template>
    </base-layout>
    
    <!-- Named slot that receives props -->
    <infinite-scroll>
      <template v-slot:item="slotProps">
        <div class="item">
          {{ slotProps.item.text }}
        </div>
      </template>
    </infinite-scroll>
    
    <!-- Default slot that receive props, with destructuring -->
    <mouse-position v-slot="{ x, y }">
      Mouse position: {{ x }}, {{ y }}
    </mouse-position>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28

    For more details, see the links below.

  • See also:

# v-pre

  • Does not expect expression

  • Usage:

    Skip compilation for this element and all its children. You can use this for displaying raw mustache tags. Skipping large numbers of nodes with no directives on them can also speed up compilation.

  • Example:

    <span v-pre>{{ this will not be compiled }}</span>
    
    1

# v-cloak

  • Does not expect expression

  • Usage:

    This directive will remain on the element until the associated component instance finishes compilation. Combined with CSS rules such as [v-cloak] { display: none }, this directive can be used to hide un-compiled mustache bindings until the component instance is ready.

  • Example:

    [v-cloak] {
      display: none;
    }
    
    1
    2
    3
    <div v-cloak>
      {{ message }}
    </div>
    
    1
    2
    3

    The <div> will not be visible until the compilation is done.

# v-once

  • Does not expect expression

  • Details:

    Render the element and component once only. On subsequent re-renders, the element/component and all its children will be treated as static content and skipped. This can be used to optimize update performance.

    <!-- single element -->
    <span v-once>This will never change: {{msg}}</span>
    <!-- the element have children -->
    <div v-once>
      <h1>comment</h1>
      <p>{{msg}}</p>
    </div>
    <!-- component -->
    <my-component v-once :comment="msg"></my-component>
    <!-- `v-for` directive -->
    <ul>
      <li v-for="i in list" v-once>{{i}}</li>
    </ul>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    Since 3.2, you can also memoize part of the template with invalidation conditions using v-memo.

  • See also:

# v-memo 3.2+

  • Expects: Array

  • Details:

    Memoize a sub-tree of the template. Can be used on both elements and components. The directive expects a fixed-length array of dependency values to compare for the memoization. If every value in the array was the same as last render, then updates for the entire sub-tree will be skipped. For example:

    <div v-memo="[valueA, valueB]">
      ...
    </div>
    
    1
    2
    3

    When the component re-renders, if both valueA and valueB remain the same, all updates for this <div> and its children will be skipped. In fact, even the Virtual DOM VNode creation will also be skipped since the memoized copy of the sub-tree can be reused.

    It is important to specify the memoization array correctly, otherwise we may skip updates that should indeed be applied. v-memo with an empty dependency array (v-memo="[]") would be functionally equivalent to v-once.

    Usage with v-for

    v-memo is provided solely for micro optimizations in performance-critical scenarios and should be rarely needed. The most common case where this may prove helpful is when rendering large v-for lists (where length > 1000):

    <div v-for="item in list" :key="item.id" v-memo="[item.id === selected]">
      <p>ID: {{ item.id }} - selected: {{ item.id === selected }}</p>
      <p>...more child nodes</p>
    </div>
    
    1
    2
    3
    4

    When the component's selected state changes, a large amount of VNodes will be created even though most of the items remained exactly the same. The v-memo usage here is essentially saying "only update this item if it went from non-selected to selected, or the other way around". This allows every unaffected item to reuse its previous VNode and skip diffing entirely. Note we don't need to include item.id in the memo dependency array here since Vue automatically infers it from the item's :key.

    WARNING

    When using v-memo with v-for, make sure they are used on the same element. v-memo does not work inside v-for.

    v-memo can also be used on components to manually prevent unwanted updates in certain edge cases where the child component update check has been de-optimized. But again, it is the developer's responsibility to specify correct dependency arrays to avoid skipping necessary updates.

  • See also:

# v-is deprecated

Deprecated in 3.1.0. Use is attribute with vue: prefix instead.