Complete the code to import the lifecycle hook for setup.
import { [1] } from 'vue';
In Vue 3, onMounted is the lifecycle hook used inside composables or setup functions.
Complete the code to define a composable that logs a message when mounted.
export function useLogger() {
[1](() => {
console.log('Component mounted');
});
}mounted as a function instead of a hook.The onMounted hook runs the callback when the component using the composable is mounted.
Fix the error in the composable to clean up on unmount.
import { onMounted, [1] } from 'vue'; export function useTimer() { let timer; onMounted(() => { timer = setInterval(() => console.log('tick'), 1000); }); [1](() => { clearInterval(timer); }); }
onDestroy which is not a Vue hook.beforeUnmount without importing it.onUnmounted is the Vue lifecycle hook to run cleanup code when the component is removed.
Fill both blanks to create a composable that tracks window width and cleans up on unmount.
import { ref, [1], [2] } from 'vue'; export function useWindowWidth() { const width = ref(window.innerWidth); function updateWidth() { width.value = window.innerWidth; } [1](() => { window.addEventListener('resize', updateWidth); }); [2](() => { window.removeEventListener('resize', updateWidth); }); return width; }
watch instead of lifecycle hooks for event listeners.onMounted adds the event listener when the component mounts, and onUnmounted removes it when the component unmounts.
Fill all three blanks to create a composable that logs on mount, updates a count on button click, and cleans up on unmount.
import { ref, [1], [2], onUnmounted } from 'vue'; export function useCounter() { const count = ref(0); [1](() => { console.log('Counter mounted'); }); function increment() { count.value++; } [3](() => { console.log('Counter unmounted'); }); return { count, increment }; }
watch instead of onUpdated for update lifecycle.onMounted logs when mounted, onUpdated can track updates (though not used here), and onUnmounted logs on unmount.