Class与Style绑定
绑定HTML Class
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定HTML Class</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<style>
.text {
font-size: 30px
}
.active {
background: red;
}
.green {
color: #00ff00;
}
</style>
</head>
<body>
<div id="app">
<div class='text'
v-bind:class="{active: isActive, green: isGreen}"
style="width:200px; height:200px;text-align: center; line-height: 200px;">
hi vue1
</div>
<div class='text'
v-bind:class="classObject"
style="width:200px; height:200px;text-align: center; line-height: 200px;">
hi vue2
</div>
<div class='text'
v-bind:class="[ isActive? activeClass: '', greenClass]"
style="width:200px; height:200px;text-align: center; line-height: 200px;">
hi vue3
</div>
</div>
</body>
<script>
var vm = new Vue({
el: '#app',
data: {
isActive: true,
isGreen: true,
activeClass: 'active',
greenClass: 'green'
},
computed: {
classObject: function () {
return {
active: this.isActive,
green: this.isGreen
}
}
}
})
</script>
</html>

绑定内联样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绑定内联样式</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div id="app1">
<div v-bind:style="{color:color, fontSize:size, background: isRed ? '#FF0000' : ''}">
hi vue1
</div>
<div v-bind:style="styleObject">
hi vue2
</div>
<div v-bind:style="[styleObject, styleObject2]">
hi vue3
</div>
</div>
<script>
var vm = new Vue({
el: "#app1",
data: {
color: "#FFFFFF",
size: '50px',
isRed: true,
styleObject: {
color: 'red',
fontSize: '33px'
},
styleObject2: {
background: 'blue'
}
}
})
</script>
</body>
</html>
