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>

运行结果:

温馨提示:

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

相关推荐
心随雨下8 小时前
TypeScript中extends与implements的区别
前端·javascript·typescript
摇滚侠8 小时前
Vue 项目实战《尚医通》,底部组件拆分与静态搭建,笔记05
前端·vue.js·笔记·vue
双向338 小时前
CANN训练营实战指南:从算子分析到核函数定义的完整开发流程
前端
caleb_5208 小时前
vue cli的介绍
前端·javascript·vue.js
Swift社区8 小时前
如何监测 Vue + GeoScene 项目中浏览器内存变化并优化性能
前端·javascript·vue.js
WYiQIU8 小时前
大厂前端岗重复率极高的场景面试原题解析
前端·javascript·vue.js·react.js·面试·状态模式
zbhbbedp282793cl8 小时前
unique_ptr和shared_ptr有何区别?
java·开发语言·jvm
.NET修仙日记8 小时前
第四章:C# 面向对象编程详解:从类与对象到完整项目实践
开发语言·c#·.net·源码·教程·.net core
IT_陈寒8 小时前
Redis 高并发实战:我从 5000QPS 优化到 5W+ 的7个核心策略
前端·人工智能·后端
m0_738120728 小时前
内网横向靶场——记录一次横向渗透(三)
开发语言·网络·安全·web安全·网络安全·php