绑定 HTML class
<div :class="{ active: isActive }"></div>
const isActive = ref(true)
const hasError = ref(false)
<div class="static" :class="{ active: isActive, 'text-danger': hasError }"></div>
const classObject = reactive({
active: true,
'text-danger': false
})
<div :class="classObject"></div>
const isActive = ref(true)
const error = ref(null)
const classObject = computed(() => ({
active: isActive.value && !error.value,
'text-danger': error.value && error.value.type === 'fatal'
}))
<div :class="classObject"></div>
const activeClass = ref('active')
const errorClass = ref('text-danger')
<div :class="[activeClass, errorClass]"></div>
<div :class="[isActive ? activeClass : '', errorClass]"></div>
<div :class="[{ activeClass: isActive }, errorClass]"></div>
<p class="foo bar">Hi!</p>
* 在使用时添加一些 class:
<MyComponent class="baz boo" />
* 渲染出的 HTML 为:
<p class="foo bar baz boo">Hi!</p>