class lists and inline style operating element is a common demand for data binding. Because they are property, so they can be processed with v-bind. However, string concatenation cumbersome and error-prone, and therefore, when the v-bind for handling class and style, Vue do a special enhancements, in addition to the type of expression is the result of a string, it can also be objects or arrays.
Solution occurs vue double braces {} in {} of:
<style> [v-cloak] { display: none; } </style> <div v-cloak>{{message}}</div>
class:
Object syntax:
It can be passed to v-bind: class object.
<div v-bind:class="{ active: isActive }"></div>
active presence or absence of this class will depend on the data attribute isActive is true or false.
In more properties may be passed dynamically switching a plurality of object class. In addition, v-bind: class commands may coexist with conventional class attribute.
<div
class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }"
></div>
data: {
isActive: true,
hasError: false
}
//渲染为:
<div class="static active"></div>
Binding data objects need not defined inline in a template:
<div v-bind:class="classObject"></div>
data: {
classObject: {
active: true,
'text-danger': false
}
}
The returned object can also bind a calculated property:
<div v-bind:class="classObject"></div>
data: {
isActive: true,
error: null
},
computed: {
classObject: function () {
return {
active: this.isActive && !this.error,
'text-danger': this.error && this.error.type === 'fatal'
}
}
}
Array syntax:
You can pass to an array of v-bind: class, to apply a class list.
<div v-bind:class="[activeClass, errorClass]"></div>
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
//渲染为:
<div class="active text-danger"></div>
You can also use object syntax in the array syntax:
<div v-bind:class="[{ active: isActive }, errorClass]"></div>
Used in the assembly:
When the class attribute in a custom component that class will be added to the root element of the assembly above.
If the same type is present, on a custom category class overrides the components on the root element of the assembly.
Vue.component('my-component', {
template: '<p class="foo bar">Hi</p>'
})
<my-component class="baz boo"></my-component>
//渲染为:
<p class="foo bar baz boo">Hi</p>
style:
When the v-bind: When you need to add style to use browser engine prefix CSS properties, such as transform, Vue.js will automatically detect and add the appropriate prefix.
Object syntax:
v-bind: object syntax style is very intuitive, is a JavaScript object. CSS property name can be separated by dashes or camel (in quotes) is named:
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
data: {
activeColor: 'red',
fontSize: 30
}
Direct binding template will more clearly on a style object:
<div v-bind:style="styleObject"></div>
data: {
styleObject: {
color: 'red',
fontSize: '13px'
}
}
Object Syntax often combined use calculated properties of the returned object.
Array syntax:
v-bind: array syntax style may be applied to a plurality of the same style object element:
<div v-bind:style="[baseStyles, overridingStyles]"></div>
Multiple values:
From 2.3.0 onwards can provide style binding properties of an array with multiple values, used to provide more value prefixed, write only the last value in the array to render a supported browser.
<div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>