一 vue的条件渲染
v-if
v-else-if
v-else
v-show
v-if和v-show区别
1》v-show 没有v-else
2》v-show 不支持template
3》v-show 只是来做简单的切换(css=>display)
例子1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- <div v-if="arr.length == 0">111</div>
<div v-else>2222</div> -->
<h1 v-if="arr.length==1">111</h1>
<h1 v-else-if="arr.length==2">222</h1>
<div v-else>333</div>
</div>
<script src="../vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
arr: [1, 2, 3]
},
})
</script>
</body>
</html>
例子2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- <div v-if="arr.length !=0">有数据</div>
<div v-else>没有数据</div> -->
<div v-show="bool"> 111 </div>
<button @click="show">按钮</button>
</div>
<script src="../vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
arr: [1, 2, 3],
bool: false
},
methods: {
show: function() {
if (this.bool) {
this.bool = false
} else {
this.bool = true
}
}
}
})
</script>
</body>
</html>