<!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://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<style>
.basic {
color: red;
}
.a {
font-size: 30px;
}
.b {
color: blue;
}
.c {
background-color: pink;
}
</style>
</head>
<body>
<div id="app">
<!-- class绑定的字符串写法,适用场景:样式的类名不确定,需要动态指定 -->
<div class="basic" :class="classStr">class绑定的字符串写法</div>
<!-- class绑定的数组写法,适用场景:要绑定的样式的个数不确定,名字也不确定 -->
<div class="basic" :class="classArr">class绑定的数组写法</div>
<!-- class绑定的对象写法,适用场景:要绑定的样式的个数确定,名字也确定,但要动态决定用不用 -->
<div class="basic" :class="classObj">class绑定的对象写法</div>
<!--style绑定的对象写法 -->
<div class="basic" :style="styleObj">style绑定的对象写法</div>
<!--style绑定的数组写法 -->
<div class="basic" :style="styleArr">style绑定的数组写法</div>
</div>
</body>
<script>
const app = new Vue({
el: "#app",
data: {
classStr: "a",
classArr: ["b", "c"],
classObj: {
a: true,
b: true,
c: false,
},
styleObj: {
color: "green",
fontSize: "25px",
},
styleArr: [
{
color: "pink",
},
{
backgroundColor: "blue",
},
],
},
});
</script>
</html>