vue中v-if的常見使用


vue中v-if的常見使用
https://blog.csdn.net/qq_24147051/article/details/79776519

使用過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>

   
   
   
           
  • 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

效果圖:

這里寫圖片描述

這里寫圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM