一文读懂vue的生命周期

编程入门 行业动态 更新时间:2024-10-18 14:27:56

<a href=https://www.elefans.com/category/jswz/34/1769690.html style=一文读懂vue的生命周期"/>

一文读懂vue的生命周期

1、啥是vue的生命周期
每个 Vue 实例都有一个完整的生命周期,就好像每个人从呱呱坠地,要经历从儿童、少年、青年、成年到老年等生命阶段一样。一个vue实例要经历从开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、卸载等一系列过程,这个过程就是其生命周期。简单来说就是vue实例的生命周期就是从实例创建到销毁的整个过程。

2、啥是生命周期钩子函数
在vue实例的生命过程中会运行一些叫做生命周期钩子的函数,用户可以通过这些钩子函数在不同阶段添加自己的代码的。简单理解,钩子嘛,就是在某个阶段给你一个做某些处理的机会。

3、vue中钩子函数有哪些

beforeCreate 创建前

created 已创建

beforeMount 挂载前

mounted 已挂载

beforeUpdate 更新前

updated 已更新

beforeDestroy 销毁前

destroyed 已销毁

4、一图读懂vue的生命周期


 
 
啥?还没看懂?没关系,接下来我们举个栗子具体理解下:

上代码:

<body>
<div id="app"><p>{{message}}</p>
</div><script>const app = new Vue({el: '#app',data: {message: "我是消息"},beforeCreate: function() {console.group('------beforeCreate创建前状态------');console.log("%c%s", "color:red" , "el     : " + this.$el); //undefinedconsole.log("%c%s", "color:red","data   : " + this.$data); //undefinedconsole.log("%c%s", "color:red","message: " + this.message)},created: function() {console.group('------created创建完毕状态------');console.log("%c%s", "color:red","el     : " + this.$el); //undefinedconsole.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>

 

上结果:

 
 
好了,我们接下来再冷静的分析一波上面的操作

  • beforeCreate和created钩子函数间的生命周期,会进行数据的观测以及事件的初始化,同时在created的时候数据已经和data属性进行绑定了。需要注意的是,此时挂载阶段还没开始,el 属性不可见。
  • created和beforeMount钩子函数间的生命周期,首先会判断对象是否有el属性。如果有的话就继续向下编译,如果没有,则停止编译,也就意味着停止了生命周期,直到在该vue实例上调用vm.$mount(el)。
  • beforeMount和mounted 钩子函数间的生命周期,就是给vue对象添加el成员,同时替换掉挂载的DOM元素。
  • beforeUpdate和updated钩子函数间的生命周期,当vue发现data中的数据发生了改变,就会先后再调用beforeUpdate和updated钩子函数,触发相应组件进行页面的重新渲染。
  • 在beforeDestroy和destroyed钩子函数间的生命周期,beforeDestroy函数在实例销毁之前被调用,但在这一步,实例仍然完全可用。而destroyed函数在Vue 实例销毁后被调用。调用后,Vue 实例中的所有事件监听器会被清除,所有子实例会被销毁。

 
 
 
 
OK,大伙明没明白?明白了吧!

更多推荐

一文读懂vue的生命周期

本文发布于:2024-03-23 18:25:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1741365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:一文   读懂   生命周期   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!