Skip to content
Created: October 4, 2025

1. Commonly used Composition API Hooks in Vue3 ​

The core of Vue3 is Composition API, and logic reuse revolves around setup().

1️⃣ Responsive related ​

① ref ​

Create basic types of responsive data.

js
const count = ref(0)

Features:

  • Access using .value
  • Automatic unpacking in templates

Applicable scenarios:

  • basic type
  • Need to replace the entire value

② reactive ​

Create objects reactive.

js
const state = reactive({ name: 'vue' })

Features:

  • Cannot be deconstructed directly (responsiveness will be lost)
  • Suitable for complex objects

③ computed ​

Computed properties.

js
const total = computed(() => count.value * 2)

Features:

  • cache
  • dependency tracking
  • Support getter/setter

④ watch ​

Monitor data changes.

js
watch(count, (newVal, oldVal) => {})

Advanced usage:

js
watch(
  () => state.name,
  (val) => {},
  { immediate: true, deep: true }
)

Common parameters:

  • immediate
  • deep
  • flush: 'post' | 'pre' | 'sync'

⑤ watchEffect ​

Automatically collect dependencies.

js
watchEffect(() => {
  console.log(count.value)
})

Difference:

watchwatchEffect
Manually specify dependenciesAutomatically collect dependencies
can get oldValuenot

Suitable for side effect scenarios.

2️⃣ Life cycle Hooks ​

Vue3 lifecycle must be used in setup().

Mounting phase ​

Vue3Description
onBeforeMountBefore mounting
onMountedAfter mounting
js
onMounted(() => {
  console.log('DOM ready')
})

update stage ​

Vue3Description
onBeforeUpdateBefore update
onUpdatedAfter updated

Unloading phase ​

Vue3Description
onBeforeUnmountBefore unmounting
onUnmountedAfter unmounting

Commonly used for:

  • clear timer
  • Cancel monitoring
  • Release resources

Error catching ​

js
onErrorCaptured((err, instance, info) => {})

Similar to React’s ErrorBoundary.

KeepAlive related ​

HookDescription
onActivatedActivated
onDeactivatedDeactivated

Applies to:

  • Page cache recovery
  • Refresh data

3️⃣ Component Communication Hooks ​

defineProps ​

js
const props = defineProps({
  msg: String
})

defineEmits ​

js
const emit = defineEmits(['change'])

defineExpose ​

Exposed to the parent component.

provide / inject ​

Cross-level communication.

js
provide('key', value)
inject('key')

Suitable for:

  • form component
  • theme system
  • Plug-in mechanism

4️⃣ Other high-frequency Hooks ​

① nextTick ​

Executed after DOM update.

js
await nextTick()

② useAttrs ​

Get the transparent transmission properties.

③ useSlots ​

Get the slot.

④ getCurrentInstance ​

Get the component instance (for advanced scenarios).

2. Overall process of Vue3 life cycle ​

Vue3 life cycle process:

setup
↓
onBeforeMount
↓
onMounted
↓
onBeforeUpdate
↓
onUpdated
↓
onBeforeUnmount
↓
onUnmounted

3. Vue2 vs Vue3 life cycle comparison (high-frequency interviews) ​

Vue2Vue3
beforeCreatesetup
createdsetup
beforeMountonBeforeMount
mountedonMounted
beforeUpdateonBeforeUpdate
updatedonUpdated
beforeDestroyonBeforeUnmount
destroyedonUnmounted

Key points:

  • 👉 Vue3 not created
  • 👉 setup instead of beforeCreate + created

4. Bonus points for advanced answers ​

1️⃣ The essence of life cycle execution timing ​

The essence is based on:

  • effect
  • scheduler
  • job queue

Vue3 update mechanism:

  • Component updates are asynchronous batch processing
  • Based on microtask queue

2️⃣ watch flush timing ​

js
flush: 'pre'   // 默认,DOM更新前
flush: 'post'  // DOM更新后
flush: 'sync'  // 同步执行

Advanced scenario:

  • Manipulate DOM → use post
  • Do synchronization logic → sync

3️⃣ setup execution timing ​

  • Executed when the component instance is created
  • Earlier than onBeforeMount
  • None this

5. Summary of practical experience ​

  • The core is ref/reactive/computed/watch.
  • Use watchEffect to control side effects.
  • Asynchronous requests are usually onMounted.
  • Use onActivated for a large number of component caching scenarios.
  • Complex form encapsulation uses provide/inject extensively.
  • When optimizing performance, attention will be paid to the timing of use of watch flush and nextTick.