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

vue动态class

作者:索伦之刃   发布日期:2025-03-12   浏览:329

<template>
  <div :class="dynamicClass">Hello World</div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
      hasError: false
    };
  },
  computed: {
    dynamicClass() {
      return {
        active: this.isActive,
        'text-error': this.hasError
      };
    }
  }
};
</script>

<style>
.active {
  font-weight: bold;
}

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

解释说明:

  1. 模板部分

    • 使用 :class 绑定动态类名,dynamicClass 是一个计算属性,返回一个对象。
  2. 脚本部分

    • data 中定义了两个布尔类型的变量 isActivehasError
    • computed 中定义了 dynamicClass 计算属性,根据 isActivehasError 的值动态返回类名对象。如果 isActivetrue,则添加 active 类;如果 hasErrortrue,则添加 text-error 类。
  3. 样式部分

    • 定义了两个 CSS 类:active(加粗字体)和 text-error(红色文本)。

上一篇:vue生成二维码

下一篇:vue3 proxy

大家都在看

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 中文站