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

vue3 :class

作者:__Man︶ㄣ   发布日期:2025-07-22   浏览:47

<template>
  <div :class="classObject">这是一个使用 Vue 3 的 :class 指令的示例</div>
</template>

<script>
export default {
  data() {
    return {
      isActive: true,
      error: null,
    };
  },
  computed: {
    classObject() {
      return {
        active: this.isActive && !this.error,
        'text-danger': this.error && this.error.type === 'fatal',
      };
    },
  },
};
</script>

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

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

解释说明:

  • :class 是 Vue.js 中用于动态绑定 class 属性的指令。
  • 在上面的例子中,classObject 是一个计算属性,返回一个对象,根据 isActiveerror 的值来决定应用哪些类。
  • 如果 isActivetrueerrornull,则会应用 active 类。
  • 如果 error 存在且其类型为 fatal,则会应用 text-danger 类。
  • 样式部分定义了 activetext-danger 类的样式。

上一篇:vue 字符串拼接

下一篇:vue页面加载完成再执行的方法

大家都在看

vue.config.js configu

node.js vue

vue 图表组件

vue3watch监听多个变量

vue查看版本

vue3 reactive对象重新赋值

vue等待几秒

vue3 setup computed

vue screenfull

vue json.stringify

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

Laravel 中文站