Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue 绑定class

作者:淺墨   发布日期:2025-06-30   浏览:64

<template>
  <div :class="classObject">这是一个绑定了 class 的 div</div>
</template>

<script>
export default {
  data() {
    return {
      // 定义一个对象,用于动态绑定 class
      classObject: {
        active: true, // 如果为 true,则会添加 'active' 类
        'text-danger': false // 如果为 true,则会添加 'text-danger' 类
      }
    };
  }
};
</script>

<style>
.active {
  background-color: yellow;
}

.text-danger {
  color: red;
}
</style>

解释说明:

  • :class="classObject":使用 v-bind:class 指令(简写为 :)来动态绑定一个对象 classObject。这个对象的键是类名,值是一个布尔值,表示是否应用该类。
  • data() 中定义了 classObject 对象,其中 activetrue,所以会应用 active 类;text-dangerfalse,所以不会应用 text-danger 类。
  • 在样式中定义了 .active.text-danger 的样式,分别设置了背景颜色和文字颜色。

上一篇:vue 请求

下一篇:vue :deep

大家都在看

vue.config.js configu

node.js vue

vue查看版本

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

vue 遍历list

typescript vue

vue 复选框

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站