当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 对vue中v-if的常见使用方法详解

对vue中v-if的常见使用方法详解

2019年06月02日  | 移动技术网IT编程  | 我要评论

小年夜,漫步云端吻戏,见郭大爷

使用过vue的小伙伴一定使用过v-if 这个属性,但是这个属性主要是来干什么的呢,他得用途是那些?

这里我总结了一下,v-if使用一般有两个场景:

1- 多个元素 通过条件判断展示或者隐藏某个元素。或者多个元素

2- 进行两个视图之间的切换

下面我写了两个例子,是vue官方的简单实例。

第一个实例实现了 type等于不同值,a,b,c 三个元素的展示情况。

第二个例子实现了,点击按钮实现两个视图的切换。

<!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>vue中v-if的常见使用</title>
 <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script>
</head>
<script>
window.onload = function(){

 //创建一个vue实例
 var app = new vue({
 el: '#app',
 data: {
  type:'c',
  logintype:'username'
 },
 methods:{
  changelogintype(){
  let self = this;
  if(self.logintype=='username'){
   self.logintype = ''
  }else{
   self.logintype = 'username'
  }
  }
 }
 })
}

</script>
<body>
 <div id="app">
 <div style="color:red">v-if的简单实用</div>
 <template>
  <div v-if="type == 'a'">
  a
  </div>
  <div v-else-if="type=='b'">
  b
  </div>
  <div v-else>
  c
  </div>
 </template>
 <div style="color:green">v-if的弹框切换</div>
 <template v-if="logintype === 'username'">
  <label>用户名:</label>
  <input placeholder="enter your username" key="username-input">
 </template>
 <template v-else>
  <label>密码:</label>
  <input placeholder="enter your email address" key="email-input">
 </template>
 <button @click="changelogintype">切换状态</button>
 </div>
</body>
</html>

效果图:

vue v-if

vue v-if

以上这篇对vue中v-if的常见使用方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网