赞
踩
1.1、绑定class属性,由绑定的变量来决定应用哪个样式
1.2、可以将样式直接设置成Vue组件中的数据对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://unpkg.com/vue@next"></script> </head> <style> .blue{ color: blue; } .red{ color: red; } .green{ color: green; } .font{ font-size: 30px; } </style> <body> <div id="app"> <div v-bind:class="{blue:isblue,red:isred}"> 西安 </div> <div v-bind:class="[greenClass,fontClass]"> 郑州 </div> <div :class="style"> 武汉 </div> </div> <script> const obj={ data(){ return{ style:{ blue:true, red:false }, isblue:true, isred:false, greenClass:'green', fontClass:'font', textColor:'red', textFont:'35px' } } } Vue.createApp(obj).mount('#app') </script> </body> </html>
注意:v-bind可以缩写为:
<div :class=""></div>
<div :style=""></div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<style>
.blue{
color: bl
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。