技术文档中心
首页
React
Vue
TypeScript
Kotlin
React Native
Electron
Android
首页
React
Vue
TypeScript
Kotlin
React Native
Electron
Android
  • Vue2 基础

    • Vue 学习指南
    • Vue2 快速入门
    • Vue2 模板语法
    • Vue2 组件基础
    • Vue2 计算属性与侦听器
    • Vue2 生命周期
  • Vue2 进阶

    • Vue2 组件通信
    • Vue2 插槽详解
    • Vue2 混入与自定义指令
    • Vue2 过渡与动画
    • Vuex 状态管理
  • Vue2 高级

    • Vue2 性能优化
    • Vue2 服务端渲染(SSR)
    • Vue2 源码解析
  • Vue3 基础

    • Vue3 快速入门
    • Vue3 Composition API
    • Vue3 响应式系统
    • Vue3 组件基础
    • Vue3 生命周期
  • Vue3 进阶

    • Vue3 组合式函数
    • Vue3 组件通信
    • Vue3 Teleport 与 Suspense
    • Pinia 状态管理
    • Vue Router 4
  • Vue3 高级

    • Vue3 TypeScript
    • Vue3 性能优化
    • Vue3 测试
    • Vue3 SSR
    • Vue3 源码解析
  • 实战项目

    • Vue2 实战项目
    • Vue3 实战项目
    • Vue2 迁移 Vue3

Vue3 生命周期

生命周期对比

Options APIComposition API说明
beforeCreatesetup()组件创建前
createdsetup()组件创建后
beforeMountonBeforeMount挂载前
mountedonMounted挂载后
beforeUpdateonBeforeUpdate更新前
updatedonUpdated更新后
beforeUnmountonBeforeUnmount卸载前
unmountedonUnmounted卸载后
errorCapturedonErrorCaptured错误捕获
renderTrackedonRenderTracked渲染追踪
renderTriggeredonRenderTriggered渲染触发
activatedonActivatedkeep-alive 激活
deactivatedonDeactivatedkeep-alive 停用

Composition API 生命周期

<script setup>
import {
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted,
  onActivated,
  onDeactivated,
  onErrorCaptured
} from 'vue'

onBeforeMount(() => {
  console.log('Before Mount')
})

onMounted(() => {
  console.log('Mounted')
  // DOM 操作
})

onBeforeUpdate(() => {
  console.log('Before Update')
})

onUpdated(() => {
  console.log('Updated')
})

onBeforeUnmount(() => {
  console.log('Before Unmount')
  // 清理工作
})

onUnmounted(() => {
  console.log('Unmounted')
})

onActivated(() => {
  console.log('Activated')
})

onDeactivated(() => {
  console.log('Deactivated')
})

onErrorCaptured((err, instance, info) => {
  console.log('Error Captured', err)
  return false // 阻止错误继续传播
})
</script>

实际应用

API 调用

<script setup>
import { ref, onMounted } from 'vue'

const data = ref(null)

onMounted(async () => {
  const response = await fetch('/api/data')
  data.value = await response.json()
})
</script>

DOM 操作

<script setup>
import { ref, onMounted } from 'vue'

const inputRef = ref(null)

onMounted(() => {
  inputRef.value.focus()
})
</script>

<template>
  <input ref="inputRef" />
</template>

定时器管理

<script setup>
import { onMounted, onUnmounted } from 'vue'

let timer

onMounted(() => {
  timer = setInterval(() => {
    console.log('tick')
  }, 1000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>

事件监听

<script setup>
import { onMounted, onUnmounted } from 'vue'

const handleResize = () => {
  console.log('window resized')
}

onMounted(() => {
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

父子组件生命周期顺序

加载渲染

父 setup
父 onBeforeMount
子 setup
子 onBeforeMount
子 onMounted
父 onMounted

更新

父 onBeforeUpdate
子 onBeforeUpdate
子 onUpdated
父 onUpdated

卸载

父 onBeforeUnmount
子 onBeforeUnmount
子 onUnmounted
父 onUnmounted
最近更新: 2026/1/27 15:51
Contributors: hailong
Prev
Vue3 组件基础