Recall & Review
beginner
What does the
onMounted lifecycle hook do in Vue 3?It runs a function right after the component is added to the page (DOM). Think of it like saying "Hey, I am ready now!"
Click to reveal answer
beginner
When is the
onUnmounted hook called in a Vue component?It runs a function just before the component is removed from the page. It's like cleaning up your desk before leaving.
Click to reveal answer
beginner
How do you import <code>onMounted</code> and <code>onUnmounted</code> in Vue 3 Composition API?You import them from 'vue' like this: <br><code>import { onMounted, onUnmounted } from 'vue'</code>Click to reveal answer
intermediate
Why is
onUnmounted useful in Vue components?It helps you stop timers, remove event listeners, or clean resources so your app stays fast and tidy.
Click to reveal answer
intermediate
Show a simple example of using
onMounted and onUnmounted in a Vue component.Example:<br><pre>import { ref, onMounted, onUnmounted } from 'vue'
export default {
setup() {
const count = ref(0)
let timer
onMounted(() => {
timer = setInterval(() => {
count.value++
}, 1000)
})
onUnmounted(() => {
clearInterval(timer)
})
return { count }
}
}</pre>Click to reveal answer
What is the main purpose of
onMounted in Vue 3?✗ Incorrect
onMounted runs after the component is added to the page (DOM), so option A is correct.
Which hook should you use to clean up timers or event listeners in Vue 3?
✗ Incorrect
onUnmounted is used to clean up before the component is removed, so A is correct.
How do you import
onMounted in a Vue 3 component?✗ Incorrect
You import onMounted from 'vue' using curly braces, so C is correct.
When does
onUnmounted run in the component lifecycle?✗ Incorrect
onUnmounted runs just before the component is removed from the DOM, so B is correct.
Which of these is a good use case for
onMounted?✗ Incorrect
onMounted is perfect for starting things like timers when the component appears, so D is correct.
Explain in your own words what
onMounted and onUnmounted hooks do in Vue 3.Think about when you start and stop something when entering or leaving a room.
You got /3 concepts.
Describe a practical example where you would use both
onMounted and onUnmounted in a Vue component.Imagine turning on a light when you enter a room and turning it off when you leave.
You got /3 concepts.