一聚教程网:一个值得你收藏的教程网站

热门教程

对vue v-if v-else-if v-else 的简单使用详解

时间:2022-06-25 15:35:09 编辑:袖梨 来源:一聚教程网

首先vue.js请注意 2.1.0版本以上方可使用v-else-if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<meta charset="UTF-8">
<title></title>
<script src="../vue.js"></script>
 
 
 
<div id="box">
 
<!--实例1 vue 2.1.0以上版本支持 v-if v-else-if -->
<div v-if="type === 'A'">
 A
</div>
<div v-else-if="type === 'B'">
 B
</div>
<div v-else-if="type === 'C'">
 C
</div>
<div v-else="">
 Not A/B/C
</div>
<hr>
 
<!--实例2 v-if / v-else-->
<div v-if="type==='A'">ok!!!</div>
<div v-else="">no!!!</div>
<hr>
 
<!--实例3 模板中使用v-if / v-else-->
<my-form :login-type="loginType"></my-form>
<button @click="toggleFun">toggle loginType</button>
 
 
</div>
 
<script>
 
var MyForm = {
//template:"#myForm"
props:['loginType'],
template:`
 <div v-if="loginType === 'username'">
 <label>Username</label>
 <input placeholder="Enter your username" key="username-input"/>
 </div>
 <div v-else>
 <label>Email</label>
 <input placeholder="Enter your email address" key="email-input"/>
 </div>
`
}
 
var app = new Vue({
el:'#box',// ().$mount("#box");
data:{
 type:'C',
 loginType:'username'
},
components:{
 "my-form":MyForm
},
methods:{
 toggleFun: function() {
 this.loginType = this.loginType === 'username'? 'email':'username';
 }
},
created:function (){
}
});
</script>
<script defer="" src="https://static.cloudflareinsights.com/beacon.min.js/vcd15cbe7772f49c399c6a5babf22c1241717689176015" integrity="sha512-ZpsOmlRQV6y907TI0dKBHq9Md29nnaEIPlkf84rnaERnq6zvWvPUqr2ft8M1aS28oN72PdrCzSjY4U6VaAw1EQ==" data-cf-beacon="{"rayId":"92c4e55c5bf2e7f5","version":"2025.3.0","r":1,"serverTiming":{"name":{"cfExtPri":true,"cfL4":true,"cfSpeedBrain":true,"cfCacheStatus":true}},"token":"09a15e901f154c8c900d5950c76ccb92","b":1}" crossorigin="anonymous"></script>

页面展示如下:

vue.js下载:https://github.com/vuejs/vue

热门栏目