The correct syntax for onBeforeUnmount in Vue 3 Composition API:
import { onBeforeUnmount } from 'vue'
onBeforeUnmount(() => {
// cleanup logic here
})
Type signature:
function onBeforeUnmount(callback: () => void): void
Key characteristics:
- Called right before a component instance is unmounted
- The component instance is still fully functional when this hook runs
- Not called during server-side rendering
- Used for cleanup operations that need to happen before unmounting
Usage example:
<script setup>
import { onBeforeUnmount } from 'vue'
onBeforeUnmount(() => {
console.log('Component is about to unmount')
// Clean up resources, cancel timers, etc.
})
</script>
Sources: