2018-09-14 vue.js

http://www.lxweimin.com/p/1dd38741f8d0 上一張網址

vue.js v-bind v-show

1.v-bind

class 與 style 是 HTML 元素的屬性,用于設置元素的樣 式,我們可以用 v-bind 來設置樣式屬性。

Vue.js v-bind 在處理 class 和 style 時, 專門增強了它。表達式的結果類型除了字符串之外,還可以是對象或數組。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../diyitain/js/vue.js"></script>
</head>
<body>
    <div class="box">
        <img :src="url" alt="">
        <a href=""></a>
    </div>
    <script>
        new Vue({
            el:'.box',
            data:{
                url:'timg.jpg',
                aa:''
            }
        })
    </script>
</body>
</html>
-----------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../diyitain/js/vue.js"></script>
</head>

<body>
    <div class="box"> <img v-bind:src="url" alt="" v-on:click='but'> </div>
    <script>
        new Vue({
            el: '.box'
            , data: {
                url: 'timg.jpg'
                , flag: true
            }
            , methods: {
                but: function () {
                    if (this.flag) {
                        this.url = 'timg.jpg'
                            , this.flag = false
                    }
                    else {
                        this.url = '1.jpg'
                            , this.flag = true
                    }
                }
            }
        })
    </script>
</body>

</html>
==========================================================================================
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../diyitain/js/vue.js"></script>
    <style>
        .box {
            overflow: hidden;
        }
        .box ul li{
            float: left;
            border: 1px #000 solid;
            padding: 10px 20px;
            list-style: none;
        }
    </style>
</head>
<body>
   <div class="box">
       <img :src="url" alt="">
       <ul>
           <li v-for='(velue,index) in arr' v-on:click='but(index)'>{{index+1}}</li>
       </ul>
   </div>
    <script>
        new Vue({
            el:'.box',
            data:{
                url:'bg1.jpg',
                arr:['bg1.jpg','bg2.jpg','3.jpg','bg4.jpg','5.jpg']
            },
            methods:{
                but:function(ind){
                    this.url=this.arr[ind]
                }
            }
        })
    </script>
</body>
</html>

2. v-show 顯示與隱藏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../diyitain/js/vue.js"></script>
</head>
<body>
    <div class="box">
        <h1> {{msg}} </h1>
        <h3 v-show='!see'> {{msg}} </h3>
    </div>
    <script >
        new Vue({
            el:'.box',
            data:{
                msg:'ni',
                see:true
            }
        })
    </script>
</body>
</html>
-------------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../diyitain/js/vue.js"></script>
    <style>
        .list{
            background-color: #000;
            width: 200px;
            height: 200px;
        }
    </style>
</head>
<body>
   <div class="box">
       <button v-on:click='but'>qqq</button>
       <div class="list" v-show='see'></div>
   </div>
    <script>
          new Vue({
              el:'.box',
              data:{
                  see:true
              },
              methods:{
//                  but:function(){
//                      this.see=!this.see
//                  }
                  but:function(){
                      if(this.see){
                          this.see=false
                      }else{
                          this.see=true
                      }
                  }
              }
          }) 
    </script>
</body>
</html>

3. v-if v-else v-els-if 條件判斷

1.v-if 條件判斷使用 v-if 指令

2. v-else 可以用 v-else 指令給 v-if 添加一個 "else" 塊

3.v-else-if 在 2.1.0 新增,顧名思義,用作 v-if 的 else-if 塊。可以鏈式的多次使用

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Vue </title>
    <script src="../diyitain/js/vue.js"></script>
</head>

<body>
    <div id="app">
        <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>
    </div>
    <script>
        new Vue({
            el: '#app', 
            data: {
                type: 'C'
            }
        })
    </script>
</body>

</html>
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. Vue 實例 1.1 創建一個Vue實例 一個 Vue 應用由一個通過 new Vue 創建的根 Vue 實...
    王童孟閱讀 1,033評論 0 2
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內容,還有我對于 Vue 1.0 印象不深的內容。關于...
    云之外閱讀 5,080評論 0 29
  • Vue.js是什么 Vue.js(讀音 /vju?/, 類似于 view) 是一套構建用戶界面的 漸進式框架。與其...
    魚魚吃貓貓閱讀 3,294評論 1 12
  • Vue 實例 屬性和方法 每個 Vue 實例都會代理其 data 對象里所有的屬性:var data = { a:...
    云之外閱讀 2,244評論 0 6
  • 書總能讓我在讀時深刻體會人生諸多道理,而到現實時卻又被那不可壓制的脾氣制服。每次都在爆發和懊惱中惡性循環,真正讓自...
    a06b93caddc4閱讀 531評論 0 0