Vue生命周期理解

it2023-11-11  73

看了很多篇理解生命周期的文章,自己就整理一下,还是有很多地方不是很懂:比如beforeMount和mounted. 希望给点意见改进改进

借用网上一段代码:参考文章

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>vue生命周期学习</title> <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script> </head> <body> <div id="app"> <h1>{{message}}</h1> </div> </body> <script> var vm = new Vue({ el: "#app", data: { message: "Vue的生命周期", }, 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> </html>

beforeCreate:

在实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用

此时el和data都还未初始化,所以无法访问data, computed, watch, methods上的数据和方法。

created:

在实例创建完成后被立即调用。在这一步,实例已完成以下的配置:数据观测 (data observer),property 和方法的运算,watch/event 事件回调。然而,挂载阶段还没开始,$el property 目前尚不可用。

可以访问data, computed, watch, methods上的方法和数据,但是未挂载到DOM,不能访问$el属性,因此$refs属性为空。

在这里常用于简单的axios请求

template对生命周期的影响

1. 如果vue实例中有template参数选项
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>vue生命周期学习</title> <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script> </head> <body> <div id="app"> <h1>{{message + '---这是在outer HTML中的'}}</h1> </div> </body> <script> var vm = new Vue({ el: "#app", template: "<h1>{{message +'---这是在template中的'}}</h1>", //在vue配置项中修改的 data: { message: "Vue的生命周期", } }); </script> </html>

页面显示: 把template去掉后,页面显示: 所以,可以看到template的优先级要高于el。下面图示中对el的判断在template之前,这是因为vue需要通过el找到对应的outer html

render函数

new Vue({ el: '#app', render: function(createElement) { return createElement('h1', 'this is createElement') } })

如果添加一个render函数,则页面渲染为: 如果render和template和el同时存在: 则显示的还是上图,这说明:render函数选项 > template选项 > outer HTML. 控制台显示如下:

beforeMount:

在挂载开始之前被调用:相关的 render 函数首次被调用。这时只是虚拟DOM。

这里console.log()能打印出来有些问题,需要继续探讨

mounted:

实例被挂载后调用,这时 el 被新创建的 vm.$ el 替换了。如果根实例挂载到了一个文档内的元素上,当 mounted 被调用时 vm.$ el 也在文档内。 注意 mounted 不会保证所有的子组件也都一起被挂载。如果你希望等到整个视图都渲染完毕,可以在 mounted 内部使用 vm.$nextTick:

mounted: function () { this.$nextTick(function () { // Code that will run only after the // entire view has been rendered }) }

这时实例才挂载完成 看到和之前不同的是,message被数据渲染。在这里也可以请求数据

beforeUpdate

数据更新时调用,发生在虚拟 DOM 打补丁之前。这里适合在更新之前访问现有的 DOM,比如手动移除已添加的事件监听器。

可以监听到data的变化但是view层没有被重新渲染,view层的数据没有变化。等到updated的时候view层才被重新渲染,数据更新。

updated

由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。 当这个钩子被调用时,组件 DOM 已经更新,所以你现在可以执行依赖于 DOM 的操作。然而在大多数情况下,你应该避免在此期间更改状态。如果要相应状态改变,通常最好使用计算属性或 watcher 取而代之。

注意 updated 不会保证所有的子组件也都一起被重绘。如果你希望等到整个视图都重绘完毕,可以在 updated 里使用vm.$nextTick:

updated: function () { this.$nextTick(function () { // Code that will run only after the // entire view has been re-rendered }) }

activated

被 keep-alive 缓存的组件激活时调用。

deactivated

被 keep-alive 缓存的组件停用时调用。

beforeDestroy

实例销毁之前调用。在这一步,实例仍然完全可用。

destroyed

实例销毁后调用。该钩子被调用后,对应 Vue 实例的所有指令都被解绑,所有的事件监听器被移除,所有的子实例也都被销毁。

最新回复(0)