Trong <script setup>, lifecycle hooks được import và dùng như functions:
javascript
import { onMounted, onUpdated, onUnmounted, onBeforeMount, onBeforeUpdate, onBeforeUnmount } from 'vue'
onMounted(() => { console.log('mounted') })
onUnmounted(() => { /* cleanup */ })Mapping từ Options API (bao gồm Vue 2 → Vue 3): beforeCreate/created → code trong setup() chạy thay thế, mounted → onMounted, updated → onUpdated, beforeUpdate → onBeforeUpdate, unmounted → onUnmounted, beforeMount → onBeforeMount.
- Vue 2:
beforeDestroy→onBeforeUnmount,destroyed→onUnmounted.
Pitfall: onMounted trong SSR (Nuxt) không chạy server-side — dùng cho browser-only code.