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>
相关推荐
徐小夕7 小时前
我用 AI 撸了个开源"万能预览器":浏览器直接打开 Office、CAD 和 3D 模型
前端·vue.js·github
小码哥_常7 小时前
Flutter Android 延迟加载代码指南:提升应用性能的关键
前端
这是个栗子7 小时前
TypeScript(三)
前端·javascript·typescript·react
kvo7f2JTy7 小时前
基于机器学习算法的web入侵检测系统设计与实现
前端·算法·机器学习
北风toto7 小时前
前端CSS样式详细笔记
前端·css·笔记
nanfeiyan7 小时前
git commit
前端
前端精髓10 小时前
移除 Effect 依赖
前端·javascript·react.js
码云之上10 小时前
从一个截图函数到一个 npm 包——pdf-snapshot 的诞生记
前端·node.js·github
码事漫谈11 小时前
AI提效,到底能强到什么程度?
前端·后端
IT_陈寒11 小时前
React hooks依赖数组这个坑差点把我埋了
前端·人工智能·后端