<template>
<div :class="dynamicClass">This div has a dynamic class.</div>
</template>
<script>
export default {
data() {
return {
isActive: true,
error: false
};
},
computed: {
dynamicClass() {
return {
active: this.isActive,
'text-danger': this.error
};
}
}
};
</script>
<style>
.active {
background-color: green;
}
.text-danger {
color: red;
}
</style>
:class 是 Vue 的语法糖,用于动态绑定 class。dynamicClass 是一个计算属性,根据 isActive 和 error 的值返回不同的类名。isActive 为 true 时,active 类会被添加到元素上;当 error 为 true 时,text-danger 类会被添加到元素上。active 和 text-danger 类的样式。下一篇:vite + vue + ts
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站