美文网首页
生命周期(钩子函数)

生命周期(钩子函数)

作者: 九尾的日志 | 来源:发表于2018-08-18 20:37 被阅读0次

    Vue对象实例从初始化到销毁的整个过程。

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title>生命周期</title>
            <script src="vue.js"></script>
        </head>
    
        <body>
    
            <!-- 生命周期:vue实例从初始化到销毁的整个过程 -->
    
            <!-- 钩子函数:在生命周期内触发的各种函数 -->
    
            <div id="app">
                <p>{{ number }}</p>
                <input v-model="number" />
            </div>
            
            
            <script>
                var app = new Vue({
                    el: '#app',
                    data: {
                        number: 1
                    },
                    beforeCreate: function() {
                        console.log('初始化之前(beforeCreate)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                    created: function() {
                        console.log('初始化完成(created)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                    beforeMount: function() {
                        console.log('渲染el之前(beforeMount)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                    mounted: function() {
                        console.log('渲染el完成(beforeMount)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                    beforeUpdate: function() {
                        console.log('更新data之前(beforeUpdate)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                    updated: function() {
                        console.log('更新data完成(beforeUpdate)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                    beforeDestroy: function() {
                        console.log('销毁实例之前(beforeUpdate)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                    destroyed: function() {
                        console.log('销毁实例完成(beforeUpdate)');
                        console.log('  el:  ' + this.$el)
                        console.log('data:  ' + this.number)
                    },
                })
                
            </script>
            
        </body>
    </html>
    
    
    生命周期.png

    相关文章

      网友评论

          本文标题:生命周期(钩子函数)

          本文链接:https://www.haomeiwen.com/subject/skfqiftx.html