Vue中Class数据绑定

server/2024/10/21 10:14:26/

Class数据绑定

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

绑定对象Object

<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>

运行结果
未切换前:
在这里插入图片描述
切换后:
在这里插入图片描述

多个对象绑定

<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>

运行结果:
切换前:
在这里插入图片描述
切换后:
在这里插入图片描述

数组绑定

<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,你可以使用三元表达式。

<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>

运行结果:
在这里插入图片描述
切换:
在这里插入图片描述
数组和对象

<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>

运行结果:
在这里插入图片描述

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


http://www.ppmy.cn/server/55867.html

相关文章

【Qt】对话框

1、自定义对话框并赋予ui界面&#xff0c;用按钮呼出 https://www.bilibili.com/video/BV1rK411A7qi/?spm_id_from333.999.0.0&vd_sourcefd6555f02904e7fa85526a2ff4b8b66e 新建 - 文件和类 - Qt - Qt设计师界面类在原来的父窗口cpp文件中初始化新窗口并调用exec显示模态…

Android AlertDialog对话框

目录 AlertDialog对话框普通对话框单选框多选框自定义框 AlertDialog对话框 部分节选自博主编《Android应用开发项目式教程》&#xff08;机械工业出版社&#xff09;2024.6 在Android中&#xff0c;AlertDialog弹出对话框用于显示一些重要信息或者需要用户交互的内容。 弹出…

Symfony框架:优雅构建PHP应用的强有力工具

在PHP开发的广阔天地中&#xff0c;Symfony框架以其高性能、高安全性和组件化的特点&#xff0c;成为了构建现代Web应用的热门选择。Symfony是一个基于MVC&#xff08;模型-视图-控制器&#xff09;模式的全栈框架&#xff0c;提供了一套丰富的功能和工具&#xff0c;帮助开发者…

通过桥梁振动信号自动识别车辆(MATLAB)

只是简单参数建模&#xff0c;还没有实际场景应用。 Generation of the bridge response to multiple vehicles Initialisation clearvars;close all;clc clf;close all;Nyy 446; % Number of nodes to discretize the bridge structure. We need a spatial resolution of 1…

uniapp微信小程序电子签名

先上效果图&#xff0c;不满意可以直接关闭这页签 新建成单独的组件&#xff0c;然后具体功能引入&#xff0c;具体功能点击签名按钮&#xff0c;把当前功能页面用样式隐藏掉&#xff0c;v-show和v-if也行&#xff0c;然后再把这个组件显示出来。 【签名-撤销】原理是之前绘画时…

汽车之家论坛评论全面采集实战指南:Python爬虫篇

聚焦汽车之家&#xff0c;解锁评论宝藏 在这个数据为王的时代&#xff0c;每一个角落的信息都可能成为宝贵的洞察来源。汽车之家&#xff0c;作为汽车行业内的权威论坛&#xff0c;其海量的用户评论不仅是消费者购车的重要参考&#xff0c;也是汽车品牌与市场研究者不可忽视的…

Redis 中 Set 和 Zset 类型

目录 1.Set类型 1.1 Set集合 1.2 普通命令 1.3 集合操作 1.4 内部编码 1.5 使用场景 2.Zset类型 2.1 Zset有序集合 2.2 普通命令 2.3 集合间操作 2.4 内部编码 2.5 使用场景 1.Set类型 1.1 Set集合 集合类型也是保存多个字符串类型的元素&#xff0c;但是和列表类型不同的是&…

Android开发环境搭建

原文&#xff1a;https://blog.c12th.cn/archives/27.html Android开发环境搭建 测试&#xff1a;笔记本原装操作系统&#xff1a;Windows 10 家庭中文版 资源分享链接&#xff1a;提取码&#xff1a;qbt2 注意事项&#xff1a; 现安装版本为基于Android开发的Eclipse版&#…