1,直接傳遞一個數組
2,在數組中使用三元表達式
3,在數組中使用對象代替三元表達式
4,通過對象綁定
<head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <style> .red { color: red; } .thin { font-weight: 200; } .italic { font-style: italic; } .active { letter-spacing: 4em; } </style> </head> <body> <div id="div1"> <!--<h1 class="red thin">你好明天</h1>--> <!--第一種方式,直接傳遞一個數組,注意:這里的class需要使用v-bind做數據綁定--> <!--<h1 :class="['red', 'thin']">你好明天</h1>--> <!--在數組中使用三元表達式--> <!--<h1 :class="['red', 'thin', flag ? 'active': '']">你好明天</h1>--> <!--在數組中使用對象代替三元表達式,提高代碼可讀性--> <!--<h1 :class="['red', 'thin', {'active': flag}]">你好明天</h1>--> <!--在為class使用v-bind綁定對象時,對象的屬性是類名,由於對象的屬性可帶引號 也可不帶引號 屬性值是一個標識符--> <!--<h1 :class="{red: true, thin: true, active: true}">你好明天</h1>--> <h1 :class="obj">你好明天</h1> </div> <script> var vm = new Vue({ el: "#div1", data: { flag: true, obj: {red: true, thin: true, active: true} } }) </script> </body>