1.class
(1)基本
<div v-bind:class="{ active: isActive }"></div>
(2)绑定多个class
<div class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>
data: {
isActive: true,
hasError: false
}
//result
<div class="static active"></div>
(3)绑定对象
<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',
}
}
}
(4)绑定数组
<div v-bind:class="[activeClass, errorClass]">
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
//result
<div class="active text-danger"></div>
//如果想切换class
<div v-bind:class="[isActive ? activeClass : '', errorClass]">
//也可以用对象的语法
<div v-bind:class="[{ active: isActive }, errorClass]">
2.style
(1)直接绑定
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
data: {
activeColor: 'red',
fontSize: 30
}
(2)绑定对象
<div v-bind:style="styleObject"></div>
data: {
styleObject: {
color: 'red',
fontSize: '13px'
}
}
(3)数组语法
<div v-bind:style="[baseStyles, overridingStyles]">
(4)自动补全
比如transform会自动添加兼容性处理