v-bind:class="{样式名称:变量}"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>class属性绑定</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<style type="text/css">
.a {
width: 200px;
height: 200px;
background-color: aliceblue;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="{a: isactive}"></div>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data:{
isactive:true
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>class属性绑定</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<style type="text/css">
.a {
width: 200px;
height: 200px;
background-color: aliceblue;
}
.b {
background-color: red;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="{a: isactive,b:hasError}"></div>
<hr >
<div v-bind:class="{a: isactive,b:noError}"></div>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data:{
isactive:true,
hasError:true,
noError:false,
}
})
</script>
</body>
</html>
v-bind:class="对象名"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>class属性绑定</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<style type="text/css">
.a {
width: 200px;
height: 200px;
background-color: aliceblue;
}
.b {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="class1"></div>
<hr >
<div v-bind:class="class2"></div>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data:{
class1:{
a:true,
b:false,
},
class2:{
a:false,
b:true,
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>class属性绑定</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<style type="text/css">
.a {
width: 200px;
height: 200px;
background-color: aliceblue;
}
.b {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="[c1]"></div>
<hr >
<div v-bind:class="[c1,c2]"></div>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data:{
c1:'a',
c2:'b',
}
})
</script>
</body>
</html>