Vue2和Vue3生命周期映射关系及异同

在这里插入图片描述

生命周期映射关系表

beforeCreate -> 使用 setup()
created -> 使用 use setup()
beforeMount ->onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy-> onBeforeUnmount
destroyed ->onUnmounted
activated -> onActivated
deactivated -> onDeactivated
errorCaptured -> onErrorCaptured

映射关系说明

beforeCreate -> 使用 setup() 函数替代

Vue 2 中的 beforeCreate 钩子函数在 Vue 3 中被废弃了,使用 setup() 函数替代。在 setup() 函数中可以访问到 props、data、computed、methods 等属性。

created -> 使用 setup() 函数替代

Vue 2 中的 created 钩子函数在 Vue 3 中被废弃了,使用 setup() 函数替代。在 setup() 函数中可以访问到 props、data、computed、methods 等属性。

beforeMount -> onBeforeMount

Vue 2 中的 beforeMount 钩子函数在 Vue 3 中被重命名为 onBeforeMount。

mounted -> onMounted

Vue 2 中的 mounted 钩子函数在 Vue 3 中被重命名为 onMounted。

beforeUpdate -> onBeforeUpdate

Vue 2 中的 beforeUpdate 钩子函数在 Vue 3 中被重命名为 onBeforeUpdate。

updated -> onUpdated

Vue 2 中的 updated 钩子函数在 Vue 3 中被重命名为 onUpdated。

beforeDestroy -> onBeforeUnmount

Vue 2 中的 beforeDestroy 钩子函数在 Vue 3 中被重命名为 onBeforeUnmount。

destroyed -> onUnmounted

Vue 2 中的 destroyed 钩子函数在 Vue 3 中被重命名为 onUnmounted。

activated -> onActivated

Vue 2 中的 activated 钩子函数在 Vue 3 中被重命名为 onActivated 。

deactivated -> onDeactivated

Vue 2 中的 deactivated 钩子函数在 Vue 3 中被重命名为 onDeactivated。

errorCaptured -> onErrorCaptured

Vue 2 中的 errorCaptured 钩子函数在 Vue 3 中被重命名为 onErrorCaptured。

vue3新增生命周期钩子函数

onRenderTracked

onRenderTracked 函数会在组件渲染时被调用,它接收两个参数:target 和 key。target 是被追踪的组件实例,key是被追踪的属性名。该函数可以用来追踪组件的渲染过程,例如记录组件渲染的次数、渲染的时间等信息。

onRenderTriggered

onRenderTriggered 函数会在组件渲染时被调用,它接收一个参数:target。target是被追踪的组件实例。该函数可以用来追踪组件的渲染触发过程,例如记录组件渲染的原因、渲染的时间等信息。

如何使用

import {
    onRenderTracked, onRenderTriggered } from 'vue'
//这两个函数都是调试工具函数,只在开发环境下生效。
//可以通过在组件中使用 debugger 语句来触发这两个函数。
//在组件渲染时,这两个函数会被触发,并输出相应的信息。
//这些信息可以帮助开发者更好地理解组件的渲染过程,从而优化组件的性能。
export default {
   
  setup() {
   
    onRenderTracked((target, key) => {
   
      console.log(`Render tracked: ${
     target} - ${
     key}`)
    })

    onRenderTriggered(target => {
   
      console.log(`Render triggered: ${
     target}`)
    })

    return {
   
      // ...
    }
  }
}

相关推荐

  1. Vue3生命周期Vue2生命周期对比

    2023-12-07 15:40:03       39 阅读
  2. Vue3生命周期 VS Vue2生命周期(小记)

    2023-12-07 15:40:03       39 阅读
  3. vue3生命周期

    2023-12-07 15:40:03       5 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-07 15:40:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-07 15:40:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-07 15:40:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-07 15:40:03       20 阅读

热门阅读

  1. Python【走出棋盘】

    2023-12-07 15:40:03       32 阅读
  2. entos7系统部署elastic6.4.3版本集群

    2023-12-07 15:40:03       36 阅读
  3. Vue3组件中使用ref解决GetElementById为空的问题

    2023-12-07 15:40:03       38 阅读
  4. Vue中下载不同文件的几种方式

    2023-12-07 15:40:03       43 阅读
  5. 微信小程序中 不同页面如何传递参数

    2023-12-07 15:40:03       43 阅读
  6. STM32关键词提取

    2023-12-07 15:40:03       36 阅读
  7. 1.求两个数最大值

    2023-12-07 15:40:03       37 阅读
  8. Python里的OS模块

    2023-12-07 15:40:03       32 阅读
  9. Rust语言项目实战(九 - 完结) - 胜利与失败

    2023-12-07 15:40:03       35 阅读
  10. Ubuntu 安装高版本FFmpeg

    2023-12-07 15:40:03       48 阅读
  11. CSS选择器看一篇就够了

    2023-12-07 15:40:03       41 阅读
  12. Redis击穿(热点key失效)

    2023-12-07 15:40:03       43 阅读