vue的生命周期详解

Vue实例的生命周期

简介

此篇文章说的是最简单的单个VUE组件的生命周期。
官网中的长图诠释了Vue实例从创建,运行到销毁的整个过程。从vue实例的创建,运行,销毁期间,总是伴随着各种各样的事件,这些事件统称为生命周期

为了便于理解,再代码中创建vue实例,并在每个钩子函数中console出每个生命周期的el,data和message。此段代码可以直接复制。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
</head>
<body>

<div id="app">
    <p>{{ message }}</p>
    <h1>{{message + '这是在outer HTML中的'}}</h1>
</div>

<script type="text/javascript">

    var app = new Vue({
        el: '#app',
        data: {
            message : "xuxiao is boy"
        },
        template:"<h1>{{message +'这是在template中的'}}</h1>",
        // render: function(createElement) {
        //     return createElement('h1', 'this is createElement')
        // },
        beforeCreate: function () {
            console.group('beforeCreate 创建前状态===============》');
            console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
            console.log("%c%s", "color:red","data   : " + this.$data); //undefined
            console.log("%c%s", "color:red","message: " + this.message)
        },
        created: function () {
            console.group('created 创建完毕状态===============》');
            console.log("%c%s", "color:red","el     : " + this.$el); //undefined
            console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
            console.log("%c%s", "color:red","message: " + this.message); //已被初始化
        },
        beforeMount: function () {
            console.group('beforeMount 挂载前状态===============》');
            console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化
            console.log(this.$el);
            console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
            console.log("%c%s", "color:red","message: " + this.message); //已被初始化
        },
        mounted: function () {
            console.group('mounted 挂载结束状态===============》');
            console.log("%c%s", "color:red","el     : " + this.$el); //已被初始化
            console.log(this.$el);
            console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
            console.log("%c%s", "color:red","message: " + this.message); //已被初始化
        },
        beforeUpdate: function () {
            console.group('beforeUpdate 更新前状态===============》');
            console.log("%c%s", "color:red","el     : " + this.$el);
            console.log(this.$el);
            console.log("%c%s", "color:red","data   : " + this.$data);
            console.log("%c%s", "color:red","message: " + this.message);
        },
        updated: function () {
            console.group('updated 更新完成状态===============》');
            console.log("%c%s", "color:red","el     : " + this.$el);
            console.log(this.$el);
            console.log("%c%s", "color:red","data   : " + this.$data);
            console.log("%c%s", "color:red","message: " + this.message);
        },
        beforeDestroy: function () {
            console.group('beforeDestroy 销毁前状态===============》');
            console.log("%c%s", "color:red","el     : " + this.$el);
            console.log(this.$el);
            console.log("%c%s", "color:red","data   : " + this.$data);
            console.log("%c%s", "color:red","message: " + this.message);
        },
        destroyed: function () {
            console.group('destroyed 销毁完成状态===============》');
            console.log("%c%s", "color:red","el     : " + this.$el);
            console.log(this.$el);
            console.log("%c%s", "color:red","data   : " + this.$data);
            console.log("%c%s", "color:red","message: " + this.message)
        }
    })
</script>
</body>
</html>

输出结果分析

 

详细分析流程图

通过代码大概了解整个流程之后再回头拆开来看官网流程图。生命周期可以分为四个阶段,初始化阶段,模板编译阶段,挂在阶段,已挂载和卸载阶段。

初始化阶段

模板编译阶段

这块比较复杂,观察刚才那段代码前面部分

   var app = new Vue({
        el: '#app',
        data: {
            message : "xuxiao is boy"
        },
        template:"<h1>{{message +'这是在template中的'}}</h1>",
        // render: function(createElement) {
        //     return createElement('h1', 'this is createElement')
        // },

首先会判断有没有el选项,如果有就继续,没有就停止生命周期。随后判断是否有template选项(此段代码中有),于是就编译template选项为render function。运行代码得到网页如下

若没有template选项,就把outerHTML当做template编译为render function。(把此段代码中的template选项注释掉),得到网页如下

这是把outerHTML当作模板编译了

<div id="app">
    <p>{{ message }}</p>
    <h1>{{message + '这是在outer HTML中的'}}</h1>
</div>

如果把实例中render function选项的注释去掉,则直接用render function里的,得到网页如下

所以按优先级来说 render function>template>outerHTML

挂载阶段

已挂载和卸载

具体使用

平时用的比较多的是mouted和created

因为在created时还没完成挂载,无法通过id获得DOM元素,所以chart.js里的该操作要使用在mounted下

 

此文章非原创,转载自:vue实例的生命周期详解

多想这样啊,可是不行。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值