Vue的生命周期一起来看看
作者:Tree_wws 发布时间:2024-05-02 17:03:21
标签:Vue,生命,周期
1. 生命周期(重要)
1.1 初步认识生命周期
别名:生命周期回调函数、生命周期函数、生命周期钩子。
生命周期是什么?Vue在关键时刻帮我们调用的一些特殊名称的函数。
生命周期函数的名字不可更改,但函数内部的具体内容由程序员自行编写
生命周期函数中的this指向也是vm 或 组件实例对象。
1.2 生命周期流程(8个)
1.初始化
? 1.beforeCreate()
? 2.created()
2.挂载(页面渲染)
? 1.beforeMount()
? 2.mounted()
3.更新
? 1.beforeUpdate()
? 2.updated()
4.销毁
? 1.beforeDestory()
? 2.destoryed()
1.3 生命周期详细流程图
1.4 常用的生命周期钩子:
beforeCreate()
:可以配置全局事件总线,后面会讲到先提一嘴
mounted()
: 可以在此阶段发送ajax请求, 启动定时器、绑定自定义事件、订阅消息等异步任务【初始化操作】
beforeDestroy()
: 在此阶段做收尾工作, 如: 清除定时器、解绑自定义事件、取消订阅消息等【首尾工作】
1.4.1 关于销毁
销毁后借助Vue开发者工具看不到任何信息
销毁后自定义事件会失效,但原生DOM事件依然有效
一般不会在
beforeDestroy
操作数据,因为即使操作数据,也不会再触发更新流程了。
1.4.2 关于父子组件的生命周期
1.加载渲染的过程
父beforeCreate ==> 父created ==> 父beforeMount ==> 子beforeCreate ==> 子created ==> 子beforeMount ==> 子mounted ==> 父mounted
2.更新的过程
父beforeUpdate ==> 子beforeUpdate ==> 子updated ==> 父updated
3.销毁过程
父beforeDestroy ==> 子beforeDestroy ==> 子destroyed ==> 父destroyed
1.5小案例
<div id="root">
<!-- 让h3透明度产生过渡的效果 -->
<h3 :style="{opacity:opacity}">欢迎学习Vue!</h3>
<button @click="des">点击我销毁</button>
</div>
<script>
Vue.config.productionTip = false
let vm = new Vue({
el: '#root',
data: {
opacity: 1
},
methods: {
des(){
// 触发此函数必定调用,beforeDestroy(),destroyed()
this.$destroy()
}
},
mounted() { //挂载
/*
- 完成模板解析后并且将初始的真实的DOM元素挂载到页面后,才执行的函数
只会执行一次
- this指向Vue
- 开发中常用的方法,当我们想要读取某个属性但是读不到,就可以
将该属性绑定到共同能够访问到的元素上,例如下面定时器的例子
*/
this.timer = setInterval(() => {
this.opacity -= 0.01
if (this.opacity <= 0)
this.opacity = 1
}, 10);
},
beforeDestroy() {
console.log("beforeDestroy - 清除定时器");
clearInterval(this.timer)
},
destroyed() {
console.log("destroyed - 销毁完毕")
},
})
</script>
1.6 代码举例说明生命周期钩子
<div id="root">
<h3>n的值为:{{n}}</h3>
<button @click="add">点击我n+1</button>
<button @click="remove">点击销毁vm</button>
</div>
<script>
Vue.config.productionTip = false
let vm = new Vue({
el: '#root',
data: {
n:1
},
methods: {
add(){
this.n++
},
remove(){
this.$destroy()
}
},
beforeCreate() {
/*
此时初始化生命周期,事件等,数据代理还未开始
vm无法访问到data中的数据,methods中的方法
*/
console.log("beforeCreate");
//console.log(this.n); //undefined
// console.log(this.add()); // this.add is not a function
// debugger
},
created() {
/*
已经完成了初始化的数据监视和数据代理
vm可以访问到data中的数据和methods的方法
*/
console.log("created");
// console.log(this.n); // 1
// console.log(this.add()); // undefined
// debugger
},
beforeMount() {
/*
挂载之前,也就是图中的判断框里执行的,此阶段是Vue
在解析模板并且生成虚拟DOM存储在内存当中,页面还不能
看到解析后的样子
*/
console.log("beforeMount");
// debugger
},
mounted() {
/*
完成模板解析后并且将初始的真实的DOM元素挂载到页面后,才执行的函数
一般在此:开启一些定时器、发送网络请求、订阅消息、
绑定自定义事件等等初始化
*/
console.log("mounted");
// debugger
},
beforeUpdate() {
/*
当数据发生更新后,此时数据已经更新完成,但是页面
还是未更新的。
也就是面试所问的:页面和尚未和数据保持同步的阶段
*/
console.log("beforeUpdate");
},
updated() {
/*
在这之前会进行新旧虚拟DOM比较,最终完成页面的更新
此阶段就是页面和数据保持同步
*/
console.log("updated");
},
beforeDestroy() {
/*
当我们调用vm.$destroy时,才会执行下面两个函数,
马上要执行销毁阶段,一般在这个阶段做一些收尾操作
比如:关掉定时器,取消订阅,解绑自定义事件
*/
console.log("beforeDestroy");
},
destroyed() {
/*
所有的指令,所有的自定义事件 * 都没了(只留下原生的dom的事件)
*/
console.log("destroyed");
},
})
</script>
来源:https://blog.csdn.net/Trees__/article/details/123107444
0
投稿
猜你喜欢
- 我们一般采用photoshop等做图工具制作电视扫描线效果图片:首先做一个黑白相间的图案,然后用这个图案进行填充,再调整图层的模式或者透明度
- 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模
- 先准备好软件:一、安装Apache,配置成功一个普通网站服务器 运行下载好的“apache_2.0.55-win32-x86-no_ssl.
- 一,命名空间函数 tf.variable_scope tf.name_scope 先以下面的代码说明两者的区别# 命名空间管理函数'
- strIn 为 输入的Email地址字符串变量 返回为true或falsereturn Regex.IsMatch(strIn, @&quo
- 看了上一篇内容之后,相信对K近邻算法有了一个清晰的认识,今天的内容——手写数字识别是对上一篇内容的延续,这里也是为了自己能更熟练的掌握k-N
- 昨天带伙伴萌学习python爬虫,准备了几个简单的入门实例涉及主要知识点:web是如何交互的requests库的get、post函数的应用r
- 1.下载mysql的repo源$ wget http://repo.mysql.com/mysql-community-release-el
- pycharm是一款功能强大的python编辑器,具有跨平台性,鉴于目前最新版pycharm使用教程较少,为了节约大家摸索此IDE的时间,来
- 最近跑实验,遇到了一个问题:由于实验数据集比较多,每次跑完一个数据集就需要手动更改文件路径,再将文件传到服务器,再运行实验,这样的话效率很低
- resources文件下面有一个lang文件夹下面有一个en文件夹,这里就是后面要使用到的存放语言的语言包的地方了。下面设置app.php里
- 接触python已有一段时间了,下面针对python基础知识的使用做一完整梳理:1)避免‘\n'等特殊字符的两种方式:a)利用转义字
- 1、官网说法在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。核心点有2个:DOM 更新和
- 打开editor/filemanager/connectors/php目录下commands.php,找到FileUpload函数,在$sE
- 最近做一个项目(Asp.net+Sql Server 2000),在原来开发的机器上运行没有任何问题.但当我在另外一台机
- 创建Spring Boot应用在基于Spring来重构JSP应用之前,我们先引入Spring Boot,使之成为一个Spring Boot应
- 依次前移,实现聊友们的发言的更迭:function form1_onsubmit()if document.form1.
- 本文实例为大家分享了文字无缝滚动效果,供大家参考,具体内容如下html<dl id="marquee" class
- MySQL中concat函数 使用方法:CONCAT(str1,str2,…)返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,
- 前言:数据库是大多数 Web 应用的基础设施,只要想把数据存储下来,就离不开数据库,下面将一起学习一下如何给 Flask 应用添加数据库支持