Vue生命周期

Vue生命周期

Vue实例需要经过创建、初始化数据、编译模板、挂载DOM、渲染、更新、渲染、卸载等一系列过程,这个过程就是Vue的生命周期,在Vue的整个生命周期中提供很多钩子函数在生命周期的不同时刻调用,Vue中提供的钩子函数有beforeCreatecreatedbeforeMountmountedbeforeUpdateupdatedbeforeDestroydestroyed

示例#

在实例化Vue过程中,会直接触发的生命周期有beforeCreatecreatedbeforeMountmounted,在数据更新的过程中触发的生命周期有beforeUpdateupdated,在销毁组件的过程中触发的生命周期有beforeDestroydestroyed

Copy
<!DOCTYPE html> <html> <head> <title>Vue生命周期</title> </head> <body> <div id="app"> <div>{{msg}}</div> <button @click="updateMsg">updateMsg</button> <button @click="destroyVue">destroyVue</button> </div> </body> <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script> <script type="text/javascript"> var vm = new Vue({ el: '#app', data: { msg: 'Vue Lifecycle' }, beforeCreate: function() { console.log("beforeCreate"); console.log(this.$el); //undefined console.log(this.$data); //undefined console.log(this.msg); // undefined console.log("--------------------"); }, created: function() { console.log("created"); console.log(this.$el); //undefined console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Lifecycle console.log("--------------------"); }, beforeMount: function() { console.log("beforeMount"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); // {__ob__: Observer} console.log(this.msg); // Vue Lifecycle console.log("--------------------"); }, mounted: function() { console.log("mounted"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Lifecycle console.log("--------------------"); }, beforeUpdate: function () { console.log("beforeUpdate"); console.log(this.$el); console.log(this.$data); console.log(this.msg); debugger; console.log("--------------------"); }, updated: function () { console.log("updated"); console.log(this.$el); console.log(this.$data); console.log(this.msg); console.log("--------------------"); }, beforeDestroy: function () { console.log("beforeDestroy"); console.log(this.$el); console.log(this.$data); console.log(this.msg); console.log("--------------------"); }, destroyed: function () { console.log("destroyed"); console.log(this.$el); console.log(this.$data); console.log(this.msg); console.log("--------------------"); }, methods:{ updateMsg: function(){ this.msg = "Vue Update"; }, destroyVue: function(){ this.$destroy(); } } }) </script> </html>

beforeCreate#

Vue实例开始创建到beforeCreate钩子执行的过程中主要进行了一些初始化操作,例如组件的事件与生命周期钩子的初始化。在此生命周期钩子执行时组件并未挂载,datamethods等也并未绑定,此时主要可以用来加载一些与Vue数据无关的操作,例如展示一个loading等。

Copy
console.log("beforeCreate"); console.log(this.$el); //undefined console.log(this.$data); //undefined console.log(this.msg); // undefined console.log("--------------------");

created#

beforeCreatecreated的过程中主要完成了数据绑定的配置、计算属性与方法的挂载、watch/event事件回调等。在此生命周期钩子执行时组件未挂载到到DOM,属性$el目前仍然为undefined,但此时已经可以开始操作datamethods等,只是页面还未渲染,在此阶段通常用来发起一个XHR请求。

Copy
console.log("created"); console.log(this.$el); //undefined console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Lifecycle console.log("--------------------");

beforeMount#

createdbeforeMount的过程中主要完成了页面模板的解析,在内存中将页面的数据与指令等进行解析,当页面解析完成,页面模板就存在于内存中。在此生命周期钩子执行时$el被创建,但是页面只是在内存中,并未作为DOM渲染。

Copy
console.log("beforeMount"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); // {__ob__: Observer} console.log(this.msg); // Vue Lifecycle console.log("--------------------");

mounted#

beforeMountmounted的过程中执行的是将页面从内存中渲染到DOM的操作。在此生命周期钩子执行时页面已经渲染完成,组件正式完成创建阶段的最后一个钩子,即将进入运行中阶段。此外关于渲染的页面模板的优先级,是render函数 > template属性 > 外部HTML

Copy
console.log("mounted"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Lifecycle console.log("--------------------");

beforeUpdate#

当数据发生更新时beforeUpdate钩子便会被调用,此时Vue实例中数据已经是最新的,但是在页面中的数据还是旧的,在此时可以进一步地更改状态,这不会触发附加的重渲染过程。在上述例子中加入了debugger断点,可以观察到Vue实例中数据已经是最新的,但是在页面中的数据还是旧的。

Copy
// this.msg = "Vue Update"; console.log("beforeUpdate"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Update debugger; console.log("--------------------");

updated#

当数据发生更新并在DOM渲染完成后updated钩子便会被调用,在此时组件的DOM已经更新,可以执行依赖于DOM的操作。

Copy
// this.msg = "Vue Update"; console.log("updated"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Update console.log("--------------------");

beforeDestroy#

Vue实例被销毁之前beforeDestroy钩子便会被调用,在此时实例仍然完全可用。

Copy
// this.$destroy(); console.log("beforeDestroy"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Update console.log("--------------------");

destroyed#

Vue实例被销毁之后destroyed钩子便会被调用,在此时Vue实例绑定的所有东西都会解除绑定,所有的事件监听器会被移除,所有的子实例也会被销毁,组件无法使用,datamethods也都不可使用,即使更改了实例的属性,页面的DOM也不会重新渲染。

Copy
// this.$destroy(); console.log("destroyed"); console.log(this.$el); //<div id="app">...</div> console.log(this.$data); //{__ob__: Observer} console.log(this.msg); // Vue Update console.log("--------------------");

每日一题#

Copy
https://github.com/WindrunnerMax/EveryDay

参考#

Copy
https://www.jianshu.com/p/672e967e201c https://cn.vuejs.org/v2/guide/instance.html https://segmentfault.com/a/1190000011381906 https://segmentfault.com/a/1190000018331135 https://www.cnblogs.com/qidh/p/11431998.html https://www.cnblogs.com/wangjiachen666/p/9497749.html
posted @   WindRunnerMax  阅读(1171)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示
CONTENTS