vue3 的生命周期

简介: vue3 的生命周期

Vue3 中的生命周期钩子与 Vue2 有一些变化和调整,主要有以下几个方面:

  1. 组合式 API 中的生命周期钩子:

    • setup() 函数中可以使用的生命周期钩子有: onMountonUnmountonBeforeMountonBeforeUnmountonBeforeUpdateonUpdatedonErrorCapturedonRenderTrackedonRenderTriggered
    • 这些钩子函数需要从 Vue 中导入使用,例如 import { onMount } from 'vue'
  2. 选项式 API 中的生命周期钩子:

    • 在选项式 API 中,生命周期钩子与 Vue2 保持一致,包括 beforeCreatecreatedbeforeMountmountedbeforeUpdateupdatedbeforeUnmountunmountederrorCapturedrenderTrackedrenderTriggered
  3. 生命周期钩子的执行顺序:

    • 在组合式 API 中,生命周期钩子的执行顺序与选项式 API 有所不同。
    • 组合式 API 中的钩子函数会在相应的生命周期阶段被调用,而不是在组件实例化时一次性调用。
  4. 新增的生命周期钩子:

    • onRenderTracked: 在组件的依赖项被追踪时触发。
    • onRenderTriggered: 在组件的重新渲染被触发时调用。
    • onErrorCaptured: 当捕获到后代组件错误时被调用。
  5. 组合式 API 的使用方式:

    • setup() 函数中使用生命周期钩子函数,通过 return 对象的方式将其暴露给组件模板。
    • 例如 onMount(() => { console.log('Component mounted') })

总的来说,Vue3 中的生命周期钩子在保持 Vue2 风格的同时,也提供了更灵活、更细粒度的组合式 API 来管理组件的生命周期。开发者可以根据具体需求选择使用选项式 API 或组合式 API 的生命周期钩子。

相关文章
|
9天前
vue3【实战】语义化首页布局
vue3【实战】语义化首页布局
28 2
|
9天前
|
存储 容器
vue3【实战】来回拖拽放置图片
vue3【实战】来回拖拽放置图片
19 2
|
9天前
|
JavaScript 开发工具 开发者
vue3【提效】使用 VueUse 高效开发(工具库 @vueuse/core + 新增的组件库 @vueuse/components)
vue3【提效】使用 VueUse 高效开发(工具库 @vueuse/core + 新增的组件库 @vueuse/components)
32 1
|
9天前
|
API
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
18 1
|
9天前
|
JavaScript
vue3 【提效】自动注册组件 unplugin-vue-components 实用教程
vue3 【提效】自动注册组件 unplugin-vue-components 实用教程
17 1
|
3天前
【vue3】Argumnt of type ‘history:RouterHistory;}is not assignable to paraeter of type ‘RouterOptions‘.
【vue3】Argumnt of type ‘history:RouterHistory;}is not assignable to paraeter of type ‘RouterOptions‘.
6 0
|
3天前
|
JavaScript
【vue3】vue3中路由hash与History的设置
【vue3】vue3中路由hash与History的设置
8 0
|
3天前
|
编解码 前端开发
【Vue3】解决电脑分辨率125%、150%及缩放导致页面变形的问题
【Vue3】解决电脑分辨率125%、150%及缩放导致页面变形的问题
11 0
|
4天前
|
JavaScript
【vue】 vue 翻页时钟制作,vue2、vue3
【vue】 vue 翻页时钟制作,vue2、vue3
11 0
|
9天前
vue3 【提效】自动导入框架方法 unplugin-auto-import 实用教程
vue3 【提效】自动导入框架方法 unplugin-auto-import 实用教程
20 0