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
This directive can trigger transitions.
Set the element’s display to
none
or its original value, depending on the truthy-ness of the binding’s value.
v-class
This directive accepts an optional argument.
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:
Updates the element’s given attribute (indicated by the argument).
Example:
1
<canvasv-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
This directive accepts an optional argument.
Apply inline CSS styles to the element.
When there is no argument, the bound value can either be a String or an Object.
If it’s a String, it will be set as the element’s
style.cssText
.
If it’s an Object, each key/value pair will be set on the element’s
style
object.
Example:
1
<divv-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
<divv-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 --> <divv-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.
This directive requires an argument.
This directive requires the value to be a Function or a statement.
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
This directive can only be used on
<input>
,
<select>
or
<textarea>
elements.
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
.
This directive can trigger transitions.
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.
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
.
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:
This directive can only be used with
v-component
.
This directive accepts only keypaths, no expressions.
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: '[email protected]' } }
1 2 3 4
<my-componentv-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
This directive can only be used with
v-component
.
This directive accepts either a method name, or a single expression statement.
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.
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.
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
Can be made reactive with mustaches
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 --> <divv-partial="{{partialId}}"></div>
You can also use this syntax (which doesn’t support reactivity):
1
<div>{{> my-partial}}</div>
v-transition
Can be made reactive with mustaches
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.
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.