Vue中Class数据绑定

Class数据绑定

数据绑定的一个常见需求场景是操作CSS class列表,因为class是attribute(属性),我们可以和其他attribute一样使用v-bind 将它们和动态的字符串绑定。但是,在处理比较复杂的绑定时,通过拼接生成字符串是麻烦且容易出错的。因此,Vue 专门为class 的v-bind 用法提供了特殊的功能增强。除了字符串外,表达式的值也可以是数组或对象。

绑定对象Object

bash 复制代码
<template>
<h3>class数据绑定</h3>
<hr>
<div :class="{active:isActive,'text-danger':hasError}">isActive</div>
<button @click="exchage">切换</button>

</template>
<script>
 export default {
data(){
    return{
        isActive:true,
        hasError:true
    }
},
methods:{
    exchage(){
        this.isActive=!this.isActive
    }
}
}

</script>
<style scoped>
.active{
    color:red;
}
</style>

运行结果

未切换前:

切换后:

多个对象绑定

bash 复制代码
<template>
<h3>class数据绑定</h3>
<hr>
<div :class="classObject">isActive</div>
<button @click="exchage">切换</button>

</template>
<script>
 export default {
data(){
    return{
      classObject:{
        active:true,
       'text-danger':true
      }
    }
},
methods:{
    exchage(){

        this.classObject.active = !this.classObject.active
    }
}
}

</script>
<style scoped>
.active{
    color:rgb(132, 0, 255);
    font-size: large;
}
</style>

运行结果:

切换前:

切换后:

数组绑定

bash 复制代码
<template>
    <h3>class数据绑定</h3>
    <hr>
    <div :class="[activeClass,errorClass]">isActive</div>
    </template>
    <script>
     export default {
    data(){
        return{
           activeClass: 'active',
           errorClass:'text-danger'
        }
    },

    }

    </script>
    <style scoped>
    .active{
        color:red;
    }
    </style>

运行结果:

如果你想在数组中渲染某个class,你可以使用三元表达式。

bash 复制代码
<template>
    <h3>class数据绑定</h3>
    <hr>
    <div :class="[isActive ? 'active' : '']">isActive</div>
    <button @click="exchage">切换</button>

    </template>
    <script>
     export default {
    data(){
        return{
            isActive:true,
        }
    },
    methods:{
        exchage(){
            this.isActive=!this.isActive
        }
    }
    }

    </script>
    <style scoped>
    .active{
        color:red;
    }
    </style>

运行结果:

切换:

数组和对象

bash 复制代码
<template>
    <h3>class数据绑定</h3>
    <hr>
    <div :class="[{'active':isActive},errorClass]">isActive</div>
    <button @click="exchage">切换</button>

    </template>
    <script>
     export default {
    data(){
        return{
            isActive:true,
            errorClass:"text-danger"
        }
    },
    methods:{
        exchage(){
            this.isActive=!this.isActive
        }
    }
    }

    </script>
    <style scoped>
    .active{
        color:red;
    }
    </style>

运行结果:

温馨提示:

数组和对象的嵌套过程中,只能数组嵌套对象,不能反其道而行。

相关推荐
老王熬夜敲代码11 分钟前
C++中的atomic
开发语言·c++·笔记·面试
a努力。1 小时前
腾讯Java面试被问:String、StringBuffer、StringBuilder区别
java·开发语言·后端·面试·职场和发展·架构
长安第一美人1 小时前
php出现zend_mm_heap corrupted 或者Segment fault
开发语言·嵌入式硬件·php·zmq·工业应用开发
源码获取_wx:Fegn08951 小时前
基于springboot + vue心理健康管理系统
vue.js·spring boot·后端
韩立学长1 小时前
【开题答辩实录分享】以《基于Vue的非遗文化知识分享平台的设计与实现》为例进行选题答辩实录分享
前端·javascript·vue.js
优弧1 小时前
离开舒适区100天,我后悔了吗?
前端·后端·面试
gihigo19981 小时前
基于MATLAB的电力系统经济调度实现
开发语言·matlab
胡gh1 小时前
css的臂膀,前端动效的利器,还是布局的“隐形陷阱”?
前端·css·html
灵感菇_1 小时前
Flutter Riverpod 完整教程:从入门到实战
前端·flutter·ui·状态管理
用户21411832636021 小时前
紧急修复!Dify CVE-2025-55182 高危漏洞,手把手教你升级避坑
前端