绑定HTML Class
内联:
<div class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }"></div>
定义对象:
<div v-bind:class="classObject"></div>
定义数组
<div v-bind:class="[activeClass, errorClass]"></div>
<div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>
<div v-bind:class="[{ active: isActive }, errorClass]"></div>
// 变量定义
data: {
isActive: true,
error: null,
activeClass: 'active',
errorClass: 'text-danger'
},
1、计算属性
computed: {
classObject: function () {
return {
active: this.isActive && !this.error,
'text-danger': this.error && this.error.type === 'fatal'
}
}
}
2、绑定对象
data: {
classObject: {
active: true,
'text-danger': false
}
}
绑定HTML Style
属性
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
对象
<div v-bind:style="styleObject"></div>
数组
<div v-bind:style="[baseStyles, overridingStyles]"></div>
// 变量定义
data: {
activeColor: 'red',
fontSize: 30,
styleObject: {
color: 'red',
fontSize: '13px'
}
}