当前位置: 首页 > 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>

相关文章:

  • deepseek + embeding模型搭建本地知识库
  • 算法面试题
  • 设置libvirtd支持远程TCP连接
  • 【动手学强化学习】03马尔可夫决策过程
  • 【指令集】Nginx
  • day12摄影平台活动
  • 浏览器标签页通信实现打开新窗口播放音乐
  • BDF(MD)
  • DeepSeek学术秘籍:如何让DeepSeek辅助论证?
  • 2月第九讲“探秘Transformer系列”
  • 无人机航迹规划: 梦境优化算法(Dream Optimization Algorithm,DOA)求解无人机路径规划MATLAB
  • Android设备 网络安全检测
  • CAS单点登录(第7版)27.开发人员
  • wx061基于ssm+vue+uniapp的疫情期间学生请假与销假系统小程序
  • Vue CLI 配置与插件
  • 1.【BUUCTF】[SUCTF 2019]EasyWeb
  • 【Docker】Docker Run 中指定 `bash` 和 `sh` 参数的区别:深入解析与实践指南
  • openGauss 3.0 数据库在线实训课程16:学习逻辑结构:表管理4
  • 小白零基础如何用cursor
  • 车载诊断框架 --- ECU运行周期operation cycle
  • 中演协:五一假期全国营业性演出票房收入同比增长3.6%
  • 【社论】跑赢12级狂风,敦煌做对了什么
  • “五一”假期出入境人数达1089.6万人次,同比增长28.7%
  • “五一”从昆明机场出境1.4万人次,较去年增长7.7%
  • 侯麦:从莫扎特到贝多芬
  • 100%关税!特朗普要让美国电影100%美国制造