Directives

Reactive Directives

These directives can bind themselves to a property on the Vue instance, or to an expression which is evaluated in the context of the instance. When the value of the underlying property or expression changes, the update() function of these directives will be called asynchronously on next tick.

v-text

Updates the element’s textContent.

Internally, {{ Mustache }} interpolations are also compiled as a v-text directive on a textNode.

v-html

Updates the element’s innerHTML.

Using v-html with user-supplied data can be dangerous. It is suggested that you only use v-html when you are absolutely sure about the security of the data source, or pipe it through a custom filter that sanitizes untrusted HTML.

v-show

Set the element’s display to none or its original value, depending on the truthy-ness of the binding’s value.

v-class

If no argument is provided, it will add the binding’s value to the element’s classList, and update the class as the value changes.

If a directive argument is provided, the argument will be the class to be toggled depending on the binding value’s truthy-ness. Combined with multiple clauses this can be pretty useful:

1
2
3
4
5
<span v-class="
red : hasError,
bold : isImportant,
hidden : isHidden
">
</span>

v-attr

Updates the element’s given attribute (indicated by the argument).

Example:

1
<canvas v-attr="width:w, height:h"></canvas>

Internally, {{ Mustache }} interpolations inside attributes are compiled into computed v-attr directives.

You should use v-attr instead of mustache binding when setting the src attribute on <img> elements. Your templates are parsed by the browser before being compiled by Vue.js, so the mustache binding will cause a 404 when the browser tries to fetch it as the image’s URL.

v-style

Apply inline CSS styles to the element.

When there is no argument, the bound value can either be a String or an Object.

Example:

1
<div v-style="myStyles"></div>
1
2
3
4
5
6
7
8
9
// myStyles can either be a String:
"color:red; font-weight:bold;"
// or an Object:
{
color: 'red',
// both camelCase and dash-case works
fontWeight: 'bold',
'font-size': '2em'
}

When there is an argument, it will be used as the CSS property to apply. Combined with multiple clauses you can set multiple properties together:

Example:

1
2
3
4
5
<div v-style="
top: top + 'px',
left: left + 'px',
background-color: 'rgb(0,0,' + bg + ')'
">
</div>

v-style is also smart to detect any required browser vendor prefixes, so you can just use the un-prefixed version:

1
2
<!-- will use -webkit-transform if needed, for example -->
<div v-style="transform: 'scale(' + scale + ')'"></div>

It is recommended to use v-style instead of mustache bindings inside style attribute because Internet Explorer, regardless of version, will remove invalid inline styles when parsing the HTML.

v-on

Attaches an event listener to the element. The event type is denoted by the argument. It is also the only directive that can be used with the key filter. For more details see Listening for Events.

v-model

Create a two-way binding on a form input element. Data is synced on every input event by default. For detailed examples see Handling Forms.

v-if

Conditionally insert / remove the element based on the truthy-ness of the binding value. If the element is a <template> element, its content will be extracted as the conditional block.

Example:

1
2
3
4
<template v-if="test">
<p>hello</p>
<p>world</p>
</template>

Will render:

1
2
3
4
<!--v-if-start-->
<p>hello</p>
<p>world</p>
<!--v-if-end-->

v-repeat

Create a child ViewModel for every item in the binding Array or Object. If the value is a whole Number then that many child ViewModels are created. These child ViewModels will be automatically created / destroyed when mutating methods, e.g. push(), are called on the Array or Object, or the number is increased or decreased.

When no argument is provided, the child ViewModel will directly use the assigned element in the Array as its $data. If the value is not an object, a wrapper data object will be created and the value will be set on that object using the alias key $value.

Example:

1
2
3
4
5
<ul>
<li v-repeat="users">
{{name}} {{email}}
</li>
</ul>

If an argument is provided, a wrapper data object will always be created, using the argument string as the alias key. This allows for more explicit property access in templates:

1
2
3
4
5
<ul>
<li v-repeat="user : users">
{{user.name}} {{user.email}}
</li>
</ul>

For detailed examples, see Displaying a List.

v-with

Allows a child ViewModel to inherit data from the parents. You can either pass in an Object which will be used as the data option, or bind individual parent properties to the child with different keys. This directive must be used in combination with v-component.

Example inheriting an object:

1
2
3
4
5
6
7
// parent data looks like this
{
user: {
name: 'Foo Bar',
email: 'foo@bar.com'
}
}
1
2
3
4
<my-component v-with="user">
<!-- you can access properties without `user.` -->
{{name}} {{email}}
</my-component>

Example inheriting individual properties (using the same data):

1
<my-component v-with="myName: user.name, myEmail: user.email">
  <!-- you can access properties with the new keys -->
  {{myName}} {{myEmail}}
</my-component>

v-events

Allows a parent instance to listen to events on a child instance. The difference from v-on is that v-events listens to Vue’s component system events created via vm.$emit() rather than DOM events. This directive allows more decoupled parent-child communication without having to hard-code event listeners into the parent component. Note that it can only be used together with v-component, i.e. on the root element of a child component.

Example:

1
2
<!-- inside parent template -->
<div v-component="child" v-events="change: onChildChange"></div>

When the child component calls this.$emit('change', ...), the parent’s onChildChange method will be invoked with additional arguments passed to $emit().

Literal Directives

Literal directives treat their attribute value as a plain string; they do not attempt to bind themselves to anything. All they do is executing the bind() function with the string value once. Literal directives accept mustache expressions inside their value, but these expressions will be evaluated only once on first compile and do not react to data changes.

v-component

Compile this element as a child ViewModel with a registered component constructor. This can be used with v-with to inehrit data from the parent. For more details see Component System.

v-partial

Replace the element’s innerHTML with a registered partial. Partials can be registered with Vue.partial() or passed inside the partials option.

Using the mustache tag inside v-partial makes it reactive:

1
2
<!-- content will change based on vm.partialId -->
<div v-partial="{{partialId}}"></div>

You can also use this syntax (which doesn’t support reactivity):

1
<div>{{> my-partial}}</div>

v-transition

Notify Vue.js to apply transitions to this element. The transition classes are applied when certain transition-triggering directives modify the element, or when the Vue instance’s DOM manipulation methods are called.

For details, see the guide on transitions.

v-ref

Register a reference to a child component on its parent for easier access. Only respected when used in combination with v-component or v-repeat. The component instance will be accessible on its parent’s $ object. For an example, see child reference.

When used with v-repeat, the value will be an Array containing all the child Vue instances corresponding to the Array they are bound to.

v-el

Register a reference to a DOM element on its owner Vue instance for easier access. e.g. <div v-el="hi"> will be accessible as vm.$$.hi.

Empty Directives

Empty directives do not require and will ignore their attribute value.

v-pre

Skip compilation for this element and all its children. Skipping large numbers of nodes with no directives on them can speed up compilation.

v-cloak

This property remains on the element until the associated ViewModel 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 ViewModel is ready.