首先vue.js请注意 2.1.0版本以上方可使用v-else-if
<!DOCTYPE html>
<html>
<head>
< charset="UTF-8">
< ></ >
< src="../vue.js"></ >
</head>
<body>
<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>
< >
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 (){
}
});
</ >
</body>
</html>
页面展示如下:

vue.js下载:https://github.com/vuejs/vue
以上这篇对vue v-if v-else-if v-else 的简单使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
10个Python小技巧你值得拥有
-
走,我们一起让改变发生
2026-05-18栏目: 教程
-
互联网娱乐的风口浪尖下,老虎游戏机该如何选择?
2026-05-18栏目: 教程
-
道屹道:即时聊天APP开发 符合当前的时代潮流
2026-05-18栏目: 教程
-
微软前CEO鲍尔默,会成为下一个库克吗?商业模式错误
2026-05-18栏目: 教程
-
加速4G、5G网络演进 全“芯”展锐出新招
2026-05-18栏目: 教程
