javascript / beginner
Snippet
Component Cleanup with onUnmounted
It is important to clean up resources when a component is destroyed. onUnmounted runs just before the component is removed from the DOM, helping prevent memory leaks.
snippet.js
1
2
3
4
5
import { onUnmounted } from 'vue';onUnmounted(() => {console.log('Component is being removed. Cleaning up...');});
vue
Breakdown
1
import { onUnmounted } from 'vue';
Imports the cleanup hook used for managing component destruction.
2
onUnmounted(() => { ... });
Executes logic like stopping timers or removing global event listeners before the component disappears.