1. class与style的强制绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>04_class与style绑定</title>
<style>
.aClass {
color: red;
}
.bClass {
color: blue;
}
</style>
</head>
<body>
<div id="demo">
<h2>1. class绑定: :class='xxx'</h2>
<p :class="a">class绑定测试哈哈哈</p>
<h2>2. style绑定</h2>
<p :style="{color:activeColor,fontSize:fontSize+'px'}">:style="{color:activeColor,fontSize:fontSize+'px'}"</p>
<button @click="update">更新</button>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
new Vue({
el: "#demo",
data: {
a: "aClass",
b: "bClass",
activeColor: "red",
fontSize: 40
},
methods: {
update() {
this.a = this.b;
this.activeColor = "green";
this.fontSize = 20;
}
}
})
</script>
</body>
</html>
- 条件渲染
v-if、v-else、v-show指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="demo">
<p v-if="ok">成功</p>
<p v-else>失败</p>
<p v-show="ok">showTest1</p>
<p v-show="!ok">showTest2</p>
<button @click="change">切换</button>
</div>
<script type="text/javascript">
new Vue({
el: "#demo",
data: {
ok: true
},
methods: {
change() {
this.ok = !this.ok;
}
}
})
</script>
</body>
</html>