Vue進階

Vue實例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue實例</title>
  <script type="text/javascript" src="./vue.js"></script>
</head>
<body>
    <div id="root">
        <div @click="handleClick">
            {{message}}
        </div>
        <item></item>  
        
    </div>

    <script type="text/javascript">
        
        Vue.component('item', {
            template: '<div>hello item</div>'
        })

        var vm = new Vue({   // 根實例  
            el: '#root',   // 綁定元素
            data: {        // 數據
                message: "hello world"
            },
            methods: {    // 方法
                handleClick: function(){
                    alert("hello")
                }
            }
        })
    </script>
</body>
</html>

Vue實例生命周期函數

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue實例生命周期函數</title>
  <script type="text/javascript" src="./vue.js"></script>
</head>
<body>
    <div id="app">
        
    </div>

    <script type="text/javascript">
        // 生命周期函數就是vue實例在某一個時間點會自動執(zhí)行的函數
        var vm = new Vue({   
            el: '#app',  
            template: "<div>{{test}}</div>",
            data: {
                test: "hello world"
            },
            beforeCreate: function(){
                console.log("beforeCreate")
            },
            created: function(){
                console.log("created")
            },
            beforeMount: function(){
                console.log(this.$el)
                console.log("beforeMount")
            },
            mounted: function(){
                console.log(this.$el)
                console.log("mounted")
            },
            beforeDestroy: function(){
                console.log("beforeDestroy")
            },
            destroyed: function(){
                console.log("destroyed")
            },
            beforeUpdate:function(){
                console.log("beforeUpdate")
            },
            updated:function(){
                console.log("updated")
            }
        })

    </script>
</body>
</html>

Vue模板語法

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue模板語法</title>
  <script type="text/javascript" src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <!-- 差值表達式 -->
        <div>{{name + ' Lee'}} </div> 
        <!-- v-text 和差值表達式效果相同 -->
        <div v-text="name + ' Lee'"></div>
        <div v-html="name + ' Lee'"></div>
    </div>

    <script type="text/javascript">
        
        var vm = new Vue({   
            el: '#app',  
            data: {
                name: "Dell"
            }
        })

    </script>
</body>
</html>

Vue計算屬性、方法、偵聽器

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue計算屬性、方法、偵聽器</title>
  <script type="text/javascript" src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <!-- {{firstName + " " + lastName}} -->

        <!-- 計算屬性 -->
        <!-- {{fullName}} -->  
        <!-- {{age}} -->
        <!-- 方法 -->
        <!-- {{fullName()}} -->
        <!-- {{age}} -->
        <!-- 偵聽器 -->
        {{fullName}}
        {{age}}
    </div>

    <script type="text/javascript">
        
        var vm = new Vue({   
            el: '#app',  
            data: {
                firstName: "Dell",
                lastName: "Lee",
                fullName: 'Dell Lee',
                age: 28
            },
            // 計算屬性
            // computed: {  // 存在緩存機制,計算的屬性無變化時不會重新計算
            //  fullName: function() {
            //      console.log("計算了一次")
            //      return this.firstName + " " + this.lastName
            //  }
            // },

            // 方法
            // methods:{
            //  fullName: function(){   // 無緩存機制
            //      console.log("執(zhí)行了一次")  
            //      return this.firstName + " " + this.lastName
            //  }
            // },

            // 偵聽器
            watch:{
                firstName: function(){
                    console.log("偵聽了一次")
                    this.fullName = this.firstName + " " + this.lastName
                },
                lastName: function(){
                    console.log("偵聽了一次")
                    this.fullName = this.firstName + " " + this.lastName
                }
            }
        })

    </script>
</body>
</html>

Vue計算屬性的setter和getter

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue計算屬性的setter和getter</title>
  <script type="text/javascript" src="./vue.js"></script>
</head>
<body>
    <div id="app">
        {{fullName}}
    </div>

    <script type="text/javascript">
        
        var vm = new Vue({   
            el: '#app',  
            data: {
                firstName: "Dell",
                lastName: "Lee",
            },
            computed: {
                fullName: {
                    get: function(){
                        return this.firstName + " " + this.lastName
                    },
                    set: function(value){
                        var arr = value.split(" ")
                        this.firstName = arr[0]
                        this.lastName = arr[1]
                    }
                }
            }
        })

    </script>
</body>
</html>

Vue中的樣式綁定

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue中的樣式綁定</title>
  <script type="text/javascript" src="./vue.js"></script>
  <style type="text/css">
    .activated{
        color: red;
    }
  </style>
</head>
<body>
    <div id="app">
        <!-- <div @click="handleDivClick"
             :class="[activated, activatedOne]"
        >
            Hello world
        </div> -->
        <div :style="[styleObj,{fontSize: '20px'}]" @click="handleDivClick">
            Hello world
        </div>
    </div>

    <script type="text/javascript">
        
        var vm = new Vue({   
            el: '#app',  
            data:{
                styleObj: {
                    color: ""
                }
                // activated: "",
                // activatedOne: "activated-one"
            },
            methods:{
                // handleDivClick: function(){
                //  // if(this.activated === "activated"){
                //  //  this.activated = ""
                //  // }else{
                //  //  this.activated = "activated"
                //  // }
                //  this.activated = this.activated === "activated" ?
                //      "" : "activated"                    
                // }
                handleDivClick: function(){
                    this.styleObj.color = this.styleObj.color === "red" ?
                        "":"red"
                }
            }
        })

    </script>
</body>
</html>

Vue中的條件渲染

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue中的條件渲染</title>
  <script type="text/javascript" src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <!-- <div v-if="show === 'a'" data-test="v-if">This is a</div>  
        <div v-else-if="show === 'b'" data-test="v-if">This is b</div>  
        <div v-else>This is others </div>   -->
        <div v-if="show">
            用戶名:<input type="" name="" key="username">  
        </div> 
        <div v-else>
            郵箱名:<input type="" name="" key="password">
        </div>

        <!-- display: none v-show性能高 -->
        <!-- <div v-show="show" data-test="v-show">{{message}}</div>   -->
    </div>

    <script type="text/javascript">
        
        var vm = new Vue({   
            el: '#app',  
            data:{
                show: "a",
                message: "Hello world"
            },
            
        })

    </script>
</body>
</html>

Vue中的列表渲染

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue中的列表渲染</title>
  <script type="text/javascript" src="./vue.js"></script>
</head>
<body>
    <div id="app">
        <div v-for="item of userInfo">
            {{item}}
        </div>
    </div>

    <script type="text/javascript">
        // push pop shift unshift splice  sort reverse
        var vm = new Vue({   
            el: '#app',  
            data:{
                userInfo: [
                    1,2,3,4
                ]
            },
            
        })

    </script>
</body>
</html>
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容