vue的生命周期函数

转载自 http://www.cnblogs.com/gagag/p/6246493.htmlhtml

这是Vue文档里关于实例生命周期的解释图浏览器

那么下面咱们来进行测试一下app

<section id="app-8">
    {{data}}
</section>
复制代码
var myVue=new Vue({
        el:"#app-8",
        data:{
            data:"aaaaa",
            info:"nono"
        },
        beforeCreate:function(){
            console.log("建立前========")
            console.log(this.data)
            console.log(this.$el)
        },
        created:function(){
            console.log("已建立========")
            console.log(this.info)
            console.log(this.$el)
        },
        beforeMount:function(){
            console.log("mount以前========")
            console.log(this.info)
            console.log(this.$el)
        },
        mounted:function(){
            console.log("mounted========")
            console.log(this.info)
            console.log(this.$el)
        },
        beforeUpdate:function(){
            console.log("更新前========");

        },
        updated:function(){
            console.log("更新完成========");
        },
        beforeDestroy:function(){
            console.log("销毁前========")
            console.log(this.info)
            console.log(this.$el)
        },
        destroyed:function(){
            console.log("已销毁========")
            console.log(this.info)
            console.log(this.$el)
        }
    })
复制代码

 

代码如上,浏览器开始加载文件dom

 

 

 

 

由上图可知:测试

  一、beforeCreate 此时$el、data 的值都为undefinedthis

  二、建立以后,此时能够拿到data的值,可是$el依旧为undefinedspa

  三、mount以前,$el的值为“虚拟”的元素节点code

  四、mount以后,mounted以前,“虚拟”的dom节点被真实的dom节点替换,并将其插入到dom树中,因而在触发mounted时,能够获取到$el为真实的dom元素()htm

  myVue.$el===document.getElementById("app-8")  // trueblog

 

接着,在console中修改data,更新视图

 

 

   触发beforeUpdata 和updated

 

  接着,执行myVue.$destroy()

 

 

总结一下,对官方文档的那张图简化一下,就获得了这张图

 

 文章中如有错误请指出,转载请注明出处,谢谢~

相关文章
相关标签/搜索