.red{color: red;}.thin{font-weight:200;}.italic{font-style: italic;}.active{letter-spacing: 0.5em;word-spacing: 0.5em;}
class 需要使用 v-bind 做数据绑定
<h1 :class="['red', 'thin']">{{msg}}</h1>
<h1 :class="['red', 'thin', isactive?'active':'']">{{msg}}</h1>
<h1 :class="['red', 'thin', {'active': isactive}]">{{msg}}</h1>
<h1 :class="{red:true, italic:true, active:false, thin:true}">{{msg}}</h1><h1 :class="classObj">{{msg}}</h1>
Vue 实例对象
const vm = new Vue({el: "#app",data: {msg: '这是一个大标题!',isactive: false,classObj: {red:true, italic:true, active:false, thin:true},},});
:style 的形式,书写样式对象
<h1 :style="{color: 'red', 'font-size': '40px'}">这是一个善良的H1</h1>
data 中,并直接引用到 :style 中
const vm = new Vue({el: "#app",data: {h1StyleObj: { color: 'red', 'font-size': '40px', 'font-weight': '200' }},});
<h1 :style="h1StyleObj">这是一个善良的H1</h1>
:style 中通过数组,引用多个 data 上的样式对象
const vm = new Vue({el: "#app",data: {h1StyleObj: { color: 'red', 'font-size': '40px', 'font-weight': '200' },h1StyleObj2: { fontStyle: 'italic' }},});
<h1 :style="[h1StyleObj, h1StyleObj2]">这是一个善良的H1</h1>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号