vue的一生【生命周期函数】

本文通过实例解析Vue.js的生命周期,包括beforeCreate、created、beforeMount、mounted等各个阶段,展示了各阶段中Vue实例的状态。同时,文章探讨了如何在不同生命周期钩子中进行初始化操作、发起请求以及组件销毁等实践应用。最后,作者强调了实践在理解Vue生命周期中的重要性。

前言

生命周期听着并不陌生,但不太会用。。。
所以今天来啃啃这个硬骨头。

开始

vue.js生命周期官方文档:https://cn.vuejs.org/v2/guide/instance.html

生命周期图示:
在这里插入图片描述
在这里插入图片描述

这样子看可能还不太理解,来看看实例:

<div id="app">
        {{message}}
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                message: 'hi,i am test'
            },
            beforeCreate: function () {
                console.group('--------beforeCreate 创建前状态---------')
                console.log("%c%s", "color:red", "el :" + this.$el)
                console.log("%c%s", "color:red", "data :" + this.$data)
                console.log("%c%s", "color:red", "message :" + this.message)
            },
            created: function () {
                console.group('--------created 创建完毕状态---------')
                console.log("%c%s", "color:red", "el :" + this.$el)
                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>

在控制台显示出来的效果是怎么样的

在这里插入图片描述

beforecreated:el 和 data 并未初始化
created:完成了 data 数据的初始化,el没有
beforeMount:完成了 el 和 data 初始化
mounted :完成挂载

测试beforeUpdate与updated

更改message观察更新前与更新后的变化
在这里插入图片描述
销毁vue实例观察销毁前与销毁后的变化
在这里插入图片描述
这么多钩子函数,看着有点乱欸
举个例子该怎么用吧:
比如created:做一些初始化,实现函数自执行
mounted:发起后端请求,拿回数据。

也还是懵懵懂懂,实践得真知,以后运用到实际项目中或许就会有更清楚的认识吧。

主要参考的是这位大佬的文章然后自己实践了一下:https://segmentfault.com/a/1190000008010666

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值