Vue--绑定class样式

有三种方式:

1、字符串写法(freeA)

适用于:样式的类名不确定,需要动态指定

2、数组写法(freeB)

适用于:要绑定的样式个数不确定,名字也不确定

3、对象写法(freeC)

适用于:要绑定的样式个数确定,名字确定,但是要动态决定是否

对象写法简写

复制代码
<button style="width: 5rem;height: 3rem;"  :class="{样式名:true|false}}" @click="test"></button>
如
<button style="width: 5rem;height: 3rem;"  :class="{greeC:freeCC}" @click="test"></button>

demo如下

复制代码
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<style>
	
	.redC{
		background-color: red;
	}
	.greeC{
		background-color: gray;
	}
	.pinC{
		background-color: pink;
	}
	
</style>

<div id="app">{{ message }}
<button style="width: 5rem;height: 3rem;"  :class="freeA" @click="test"></button>
<button style="width: 5rem;height: 3rem;"  :class="freeB" @click="test"></button>
<button style="width: 5rem;height: 3rem;"  :class="freeC" @click="test"></button>
</div>
<script>
//  vue2
let vm=new Vue({
  el: '#app',
  data: {
	  	  message:'阿萨德',
	  x:'',
	  y:'',
	  z:{
		za:1
	  },
	  freeB:['redC','greeC'],
	  freeA:'redC',
	  freeC:{
		pinC:true
	  }
  },
  methods:{
	
  },
  
})

</script>
相关推荐
陈随易1 天前
MoonBit访谈:MoonBit开发moonclaw实现“养虾”自由
前端·后端·程序员
汀沿河1 天前
3 LangChain 1.0 中间件(Middleware)- after_model、after_agent
前端·中间件·langchain
紫金修道1 天前
【OpenClaw】让openclaw根据需求创造自定义skill记录
前端·javascript·chrome
mengqudoh1 天前
vue springboot mybatis实现自定义条件检索功能
vue.js·spring boot·mybatis
周杰伦fans1 天前
Edge浏览器 about:blank 问题修复
前端·数据库·edge
嘉琪0011 天前
Day6 完整学习包(async/await)——2026 0318
前端·javascript·学习
SameX1 天前
我做了个本地优先的 iOS 足迹 App,上架后才发现:最难的根本不是地图,而是让轨迹活下来
前端
踩着两条虫1 天前
AI 驱动的 Vue3 应用开发平台 深入探究(十八):扩展与定制之集成第三方库
前端·vue.js·agent
css趣多多1 天前
# Vue 3 `<script setup>` 中变量声明的正确姿势:何时必须使用 `ref()`?
前端·javascript·vue.js