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

vue2.x与vue3.x生命周期的比较

vue2.x 生命周期图示:

new Vue()
   |
   v
Init Events & Lifecycle
   |
   v
beforeCreate
   |
   v
created
   |
   v
beforeMount
   |
   v
mounted
   |
   v
beforeUpdate (when data changes)
   |
   v
updated
   |
   v
beforeDestroy (when vm.$destroy() is called)
   |
   v
destroyed

vue3.x 生命周期图示:

setup()
   |
   v
Init Events & Lifecycle
   |
   v
onBeforeMount
   |
   v
onMounted
   |
   v
onBeforeUpdate (when data changes)
   |
   v
onUpdated
   |
   v
onBeforeUnmount (when component is unmounted)
   |
   v
onUnmounted
   |
   v
onErrorCaptured (when an error is captured)

vue 2.x 版本生命周期与 vue 3.x 版本生命周期相对应的组合式 API

  • 2.x 版本使用 beforeCreate -> 3.x 版本使用 setup()
  • 2.x 版本使用 created -> 3.x 版本使用 setup()
  • 2.x 版本使用 beforeMount ->  3.x 版本使用 onBeforeMount
  • 2.x 版本使用 mounted ->  3.x 版本使用 onMounted
  • 2.x 版本使用 beforeUpdate ->  3.x 版本使用 onBeforeUpdate
  • 2.x 版本使用 updated ->  3.x 版本使用 onUpdated
  • 2.x 版本使用 beforeDestroy -> 3.x 版本使用 onBeforeUnmount
  • 2.x 版本使用 destroyed ->  3.x 版本使用 onUnmounted
  • 2.x 版本使用 errorCaptured ->  3.x 版本使用 onErrorCaptured

vue 3.x 新增的钩子函数

组合式 API 还提供了以下调试钩子函数:

  • onRenderTracked
  • onRenderTriggered

vue 2.x 版本生命周期与 vue 3.x 版本生命周期示例代码:

<template>
<div class="about"><h2>msg: {{msg}}</h2><hr><button @click="update">更新</button>
</div>
</template><script lang="ts">
import {ref,onMounted,onUpdated,onUnmounted, onBeforeMount, onBeforeUpdate,onBeforeUnmount
} from "vue"export default {beforeCreate () {console.log('beforeCreate()')},created () {console.log('created')},beforeMount () {console.log('beforeMount')},mounted () {console.log('mounted')},beforeUpdate () {console.log('beforeUpdate')},updated () {console.log('updated')},beforeUnmount () {console.log('beforeUnmount')},unmounted () {console.log('unmounted')},setup() {const msg = ref('abc')const update = () => {msg.value += '--'}onBeforeMount(() => {console.log('--onBeforeMount')})onMounted(() => {console.log('--onMounted')})onBeforeUpdate(() => {console.log('--onBeforeUpdate')})onUpdated(() => {console.log('--onUpdated')})onBeforeUnmount(() => {console.log('--onBeforeUnmount')})onUnmounted(() => {console.log('--onUnmounted')})return {msg,update}}
}
</script>
<template><h2>App</h2><button @click="isShow=!isShow">切换</button><hr><Child v-if="isShow"/>
</template><script lang="ts">
import Child from './Child.vue'
export default {data () {return {isShow: true}},components: {Child}
}
</script>

http://www.lryc.cn/news/538710.html

相关文章:

  • 接口测试及常用接口测试工具(Postman/Jmeter)
  • [论文阅读] SeeSR: Towards Semantics-Aware Real-World Image Super-Resolution
  • Python实战进阶 No1: RESTful API - 基于Flask的实例说明
  • Redis——优惠券秒杀问题(分布式id、一人多单超卖、乐悲锁、CAS、分布式锁、Redisson)
  • OpenCV机器学习(5)逻辑回归算法cv::ml::LogisticRegression
  • 百度百舸 DeepSeek 一体机发布,支持昆仑芯 P800 单机 8 卡满血版开箱即用
  • 批处理效率提升技巧
  • Kubernetes知识点总结(十)
  • 安全防御综合练习2 nat+智能选路
  • Flutter 中的数据跨层传递方案
  • 代码随想录D50-51 图论 Python
  • MyBatis进阶
  • 容器化部署Kafka的最佳实践:基于KRaft模式的无ZooKeeper方案
  • DeepSeek核心算法解析:如何打造比肩ChatGPT的国产大模型
  • LeetCode-633. 平方数之和
  • 前端面试技巧与实践
  • windows Redis Insight 如何查看宝塔docker里的redis数据
  • sql数据执行失败,三个命令依次执行
  • BGP配置华为——RR反射器配置
  • 基于Flask的艺恩影片票房分析系统的设计与实现
  • 架构设计系列(三):架构模式
  • 零基础学QT、C++(一)安装QT
  • SQL注入(SQL Injection)详解与实战
  • 【Prometheus】prometheus结合domain_exporter实现域名监控
  • Java 设计模式之命令模式
  • BT401双模音频蓝牙模块如何开启ble的透传,有什么注意事项
  • 利用二分法+布尔盲注、时间盲注进行sql注入
  • Vue 项目登录的基本流程
  • kubernetes源码分析 kubelet
  • Web3 开发者周刊 36 | 构建自主未来:Agent、可扩展性与赏金