vue生命周期简概

生命周期图
什么是生命周期:简单的讲就是数据驱动页面,页面在更新的过程就是生命周期。在这个过程中我们可以写我们的逻辑。
什么是生命周期钩子:生命周期钩子就是指的各个生命周期的回调函数

beforeCreate组件属性刚创建完,组件计算属性之前 例如data、watcher等
created实例创建完成后被立即调用,属性已绑定,但是挂载阶段还没开始,$el 属性目前尚不可用
beforeMount挂载开始之前被调用
mounted挂载之后被调用,el 被新创建的 vm.$el 替换了
beforeUpdate数据更新时调用,适合在更新之前访问现有的 DOM,比如手动移除已添加的事件监听器。
updated数据更改重新渲染和打补丁,在这之后会调用该钩子。
activated被 keep-alive 缓存的组件激活时调用
deactivated被 keep-alive 缓存的组件停用时调用
beforeDestroy实例销毁之前调用。在这一步,实例仍然完全可用。
destroyed销毁后调用
errorCaptured当捕获一个来自子孙组件的错误时被调用。
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
</head>
<body>

<div id="app">
     <p>{{ message }}</p>
</div>

<script type="text/javascript">
    
  var app = new Vue({
      el: '#app',
      data: {
          message : "xiaofan 嘻嘻嘻" 
      },
       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>

状态
console里执行以下命令,updata就会被触发
app.message= ‘yes !! I do’;
在这里插入图片描述
总结:
beforecreated:el 和 data 并未初始化
created:完成了 data 数据的初始化,el没有
beforeMount:完成了 el 和 data 初始化
mounted :完成挂载
beforeUpdate :该钩子在服务器端渲染期间不被调用,因为只有初次渲染会在服务端进行。
updataed:该钩子在服务器端渲染期间不被调用
activated/deactivated:该钩子在服务器端渲染期间不被调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值