当前位置: 首页 > news >正文

Vue学习计划-Vue2--Vue核心(七)生命周期

抛出问题:一进入页面就开启一个定时器,每隔1秒count就加1,如何实现
示例:

<body>
<div id="app">{{ n }}<button @click="add">执行</button>
</div><script>let vm = new Vue({el: "#app",data:{n: 1},methods: {add(){// this.n ++setInterval(()=>{this.n ++},1000)}},// 特定时期调用特定函数(这就是 生命周期函数/钩子函数 )// 表示页面渲染完成之后mounted(){setInterval(()=>{this.n ++},1000)   }})// 外部执行 不推荐,开启了定时器,最后要销毁的// setInterval(()=>{//     vm.n ++// },1000)
</script>
</body>

下面正式进入生命周期

1. 生命周期

  1. 常用的生命周期钩子:
    1. mounted: 发送ajax请求、启动定时器、绑定自定义事件、订阅消息等【初始化操作】
    2. beforeDestroy:清除定时器、解绑自定义事件、取消订阅消息等【收尾工作】
  2. 关于销毁Vue实例
    1. 销毁后借助Vue开发者工具看不到任何消息
    2. 销毁后自定义事件会失效
    3. 一般不会在beforeDestroy操作数据,因为即便操作数据,也不会在触发更新流程了
    4. vm.$destroy():完全销毁一个实例。清理它与其它实例的连接,解绑它的全部指令及事件监听器。
  3. vm的生命周期
    1. 将要创建 ==> 调用beforeCreate函数
    2. 创建完毕 ==> 调用created函数
    3. 将要挂载 ==> 调用beforeMount函数
    4. (重要)挂载完毕 ==> 调用mounted函数 =====>【重要的钩子】
    5. 将要更新 ==> 调用beforeUpdate函数
    6. 更新完毕 ==> 调用updated函数
    7. (重要)将要销毁 ==> 调用beforeDestroy函数 =====>【重要的钩子】
    8. 销毁完毕 ==> 调用destroyed函数
  4. 注意:
    $destory方法进入销毁生命周期,进入beforeDestroy后,销毁页面。vue-tools就不在监视,并且页面dom与Vue断了联系,点击页面事件已经无反应

在这里插入图片描述
示例:

	<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="./vue.js"></script></head><body><div id="app" :x="n"><h2>当前的n值是:{{ n }}</h2><button @click="add">添加</button><button @click="bye">销毁</button></div><script>const vm = new Vue({el: "#app",// template:'<h2>当前的n值是:{{ n }}</h2><button @click="add">添加</button> <button @click="bye">销毁</button> ', // 模板不能空格// template:`// <div>// 		<template>//     		<h2>当前的n值是:{{ n }}</h2>//   		<button @click="add">添加</button> //     		<button @click="bye">销毁</button> // 		</template>// </div>// `,// template:`// <div>//     <h2>当前的n值是:{{ n }}</h2>//     <button @click="add">添加</button> //     <button @click="bye">销毁</button> // </div>// `,data: {n: 1},methods: {add(){console.log("add")this.n ++},bye(){this.$destroy()}},beforeCreate() {console.log('beforeCreate');// console.log(this)// debugger},created() {console.log('created');// console.log(this)// debugger},beforeMount() {console.log('beforeMount');console.log(this);// 最终都不奏效// document.querySelector("h2").innerText = "哈哈"// debugger},mounted() {console.log('mounted');console.log(this.$el)// 可以修改真实DOM,不推荐// document.querySelector("h2").innerText = "哈哈"// console.log(this)// debugger},beforeUpdate() {console.log('beforeUpdate');// console.log(this.n)// debugger},updated() {console.log('updated');// this.n = 99// console.log(this.n)// debugger},beforeDestroy() {console.log('beforeDestroy');console.log(this.n)this.n = 99// debugger},destroyed() {console.log('destroyed');// console.log(this)// debugger},})// vm.$mount("#app")</script></body></html>

回归问题:beforeDestroy生命周期内清除定时器

<body>
<div id="app"><p v-text="n"></p><h2>此时的n值是:{{ n }}</h2><button @click="n=99">n值设置为99</button><button @click="bye">停止</button>
</div><script>let vm = new Vue({el: "#app",data:{n: 1},methods: {bye(){// 手动清除定时器,DOM和Vue还有联系,所以点击n=99还能实现// clearInterval()// clearInterval(this.timer)// 手动调用$destory方法进入销毁生命周期,对比区别:进入beforeDestroy后,销毁页面。vue-tools就不在监视。并且页面dom与Vue断了联系,点击n=99,已经无反应this.$destroy()}},mounted(){console.log("mounted")this.timer = setInterval(()=>{console.log('setInterval')this.n ++},1000)   },beforeDestroy(){console.log(111);clearInterval(this.timer)}})
</script>
http://www.lryc.cn/news/255486.html

相关文章:

  • 前端知识笔记(三十四)———HBuilder的下载与使用(详细步骤)
  • stl容器
  • android https 证书过期
  • lv11 嵌入式开发 中断控制器14
  • IDEA 出现问题:Idea-操作多次commit,如何合并为一个并push解决方案
  • 贝蒂的捣蛋小游戏~(C语言)
  • c# 判断是否连接公网
  • unity 2d 入门 飞翔小鸟 场景延续(八)
  • scrapy介绍,并创建第一个项目
  • Rust语言项目实战(九 - 完结) - 胜利与失败
  • 【Linux系统编程】项目自动化构建工具make/Makefile
  • harmony开发之Text组件的使用
  • using meta-SQL 使用元SQL 六
  • 如何将浮点数点左边的数每三位添加一个逗号,如 12000000.11 转化为『12,000,000.11』
  • 朴素贝叶斯 贝叶斯方法
  • 探索鸿蒙 TextInput组件
  • CNN,DNN,RNN,GAN,RL+图像处理常规算法(未完待续)
  • C# 语法笔记
  • el-table 表格多选(后端接口搜索分页)实现已选中的记忆功能。实现表格数据和已选数据(前端分页)动态同步更新。
  • Vue3自定义Hooks定义
  • 为什么Java程序员需要掌握多线程?揭秘并发编程的奥秘
  • 数组实现循环队列(新增一个空间)
  • Mysql 索引概念回顾
  • 基于SpringBoot+Vue学生成绩管理系统前后端分离(源码+数据库)
  • Hadoop集群破坏试验可靠性验证
  • Notepad++ 安装TextFx插件失败
  • 探究Logistic回归:用数学解释分类问题
  • 杨辉三角
  • MS5228/5248/5268:2.7V 到 5.5V、 12/14/16Bit、内置基准、八通道数模转换器
  • 2024年江苏省职业院校技能大赛 信息安全管理与评估 第二阶段教师组 (样卷)