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>

运行结果:

温馨提示:

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

相关推荐
Alair‎1 小时前
【无标题】
开发语言
Mr.Jessy4 小时前
JavaScript高级:构造函数与原型
开发语言·前端·javascript·学习·ecmascript
云栖梦泽6 小时前
鸿蒙应用签名与上架全流程:从开发完成到用户手中
开发语言·鸿蒙系统
白兰地空瓶6 小时前
🚀你以为你在写 React?其实你在“搭一套前端操作系统”
前端·react.js
爱上妖精的尾巴7 小时前
6-4 WPS JS宏 不重复随机取值应用
开发语言·前端·javascript
似水流年QC7 小时前
深入探索 WebHID:Web 标准下的硬件交互实现
前端·交互·webhid
陪我去看海7 小时前
测试 mcp
前端
speedoooo8 小时前
在现有App里嵌入一个AI协作者
前端·ui·小程序·前端框架·web app
全栈胖叔叔-瓜州8 小时前
关于llamasharp 大模型多轮对话,模型对话无法终止,或者输出角色标识User:,或者System等角色标识问题。
前端·人工智能
小鸡吃米…8 小时前
Python 列表
开发语言·python