第二章:vue2组件的生命周期钩子函数

一、生命周期钩子函数

对于前端的vue,react,angular的几大框架来说,说到生命周期钩子函数可能会想到react,vue中也有生命周期钩子函数

这里写图片描述

二、生命周期钩子函数

  • 1、beforeCreate组件实例刚创建,在计算属性之前(比如计算data属性)
  • 2、created组件实例创建完成,属性已经绑定,但是DOM还未生成($el属性还不存在)
  • 3、beforeMount挂载之前
  • 4、mounted被创建
  • 5、beforeUpdate组件数据更新前
  • 6、updated组件数据更新后
  • 7、activated组件被激活时调用
  • 8、deactivated组件被移动时调用
  • 9、beforeDestroy销毁之前调用
  • 10、destroyed销毁之后调用

三、书写全部的钩子函数

  • 1、html代码

    <div id="app">
        <p>{{ message }}</p>
        <input type="button" value="增加" @click="add" />
        <input type="button" value="销毁" @click="destroy" />
    </div>
  • 2、javascript代码

    var app = new Vue({
        el: '#app',
        data: {
            message: 1
        },
        methods: {
            add() {
                this.message++;
            },
            destroy() {
                app.$destroy()
            }
        },
        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)
        },
        beforeDestroy: function() {
            console.log('销毁之前');
        },
        destroyed: function() {
            console.log('销毁之后');
        }
    })
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水痕01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值