前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue生命周期

Vue生命周期

作者头像
蓓蕾心晴
发布2018-04-12 13:49:46
7730
发布2018-04-12 13:49:46
举报
文章被收录于专栏:前端小叙

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

那么下面我们来进行测试一下

代码语言:javascript
复制
<section id="app-8">
    {{data}}
</section>
代码语言:javascript
复制
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)
        }
    })

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

由上图可知:

1、beforeCreate 此时$el、data 的值都为undefined

  2、创建之后,此时可以拿到data的值,但是$el依旧为undefined

  3、mount之前,$el的值为“虚拟”的元素节点

  4、mount之后,mounted之前,“虚拟”的dom节点被真实的dom节点替换,并将其插入到dom树中,于是在触发mounted时,可以获取到$el为真实的dom元素()

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

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

   触发beforeUpdata 和updated

  接着,执行myVue.$destroy()

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-01-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档