样式绑定
设置元素的样式 用 v-bind 来设置样式属性 class 与 style 是 HTML 元素的属性
<div v-bind:class="{ active: isActive }"></div>
<div class="active"></div>
<div class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>
new Vue({
el: '#app',
data: {
isActive: true,
error: {
value: true,
type: 'fatal'
}
},
computed: {
classObject: function () {
return {
base: true,
active: this.isActive && !this.error.value,
'text-danger': this.error.value && this.error.type === 'fatal',
}
}
}
})
<div id="app">
<div v-bind:class="classObject"></div>
</div>
<div v-bind:class="[activeClass, errorClass]"></div>
<div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
v-bind:style 直接设置样式
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">
<script>
new Vue({
el: '#app',
data: {
activeColor: 'green',
fontSize: 30
}
})
</script>