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>

运行结果:

温馨提示:

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

相关推荐
w***375121 小时前
Spring 核心技术解析【纯干货版】- Ⅶ:Spring 切面编程模块 Spring-Instrument 模块精讲
前端·数据库·spring
我是阿亮啊21 小时前
搭建Vue环境遇到的问题
javascript·vue.js·npm·node.js
〝七夜56921 小时前
JVM内存结构
java·开发语言·jvm
GISer_Jing21 小时前
jx前端架构学习
前端·学习·架构
初级炼丹师(爱说实话版)21 小时前
JAVA泛型作用域与静态方法泛型使用笔记
java·开发语言·笔记
间彧1 天前
Tailwind CSS详解
前端
间彧1 天前
Headless UI详解
前端
技术净胜1 天前
MATLAB二维绘图教程:plot()函数全解析(线条样式/颜色/标记/坐标轴设置)
开发语言·matlab
Coder-coco1 天前
在线商城系统|基于springboot vue在线商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·宠物
Slow菜鸟1 天前
Java开发规范(八)| 安全规范—企业级应用的“架构级底线”
java·开发语言·安全