0
0
Vueframework~5 mins

onBeforeMount and onBeforeUnmount in Vue - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the onBeforeMount lifecycle hook in Vue?

onBeforeMount runs just before the component is added to the page. It lets you run code right before the component appears on screen.

Click to reveal answer
beginner
When does onBeforeUnmount run in a Vue component?

onBeforeUnmount runs right before the component is removed from the page. It is useful to clean up things like timers or event listeners.

Click to reveal answer
intermediate
How do onBeforeMount and onBeforeUnmount help manage resources in Vue components?
<p>They let you start or stop tasks at the right time. For example, start a timer before showing the component and stop it before removing the component to avoid memory leaks.</p>
Click to reveal answer
beginner
Show a simple example of using onBeforeMount and onBeforeUnmount in Vue's <script setup>.
<pre>import { onBeforeMount, onBeforeUnmount } from 'vue'

onBeforeMount(() =&gt; {
  console.log('Component will mount soon')
})

onBeforeUnmount(() =&gt; {
  console.log('Component will unmount soon')
})</pre>
Click to reveal answer
intermediate
Why is it important to use onBeforeUnmount to clean up in Vue components?

Cleaning up prevents problems like memory leaks or unwanted background tasks running after the component is gone. It keeps your app fast and stable.

Click to reveal answer
When does onBeforeMount run in a Vue component?
ARight after the component is removed from the DOM
BRight before the component is added to the DOM
CAfter the component is mounted
DWhen the component is updated
What is a common use for onBeforeUnmount in Vue?
ATo update the component's state
BTo start a timer when the component loads
CTo fetch data from an API
DTo clean up event listeners before the component is removed
Which Vue lifecycle hook runs after the component is mounted?
AonMounted
BonBeforeMount
ConBeforeUnmount
DonUpdated
What happens if you forget to clean up in onBeforeUnmount?
AMemory leaks or unwanted background tasks may occur
BThe component will never unmount
CThe component will mount twice
DNothing, cleanup is optional
Which of these is true about onBeforeMount and onBeforeUnmount?
ABoth run after the component is mounted
BBoth run before the component is added to the DOM
C<code>onBeforeMount</code> runs before mounting, <code>onBeforeUnmount</code> runs before unmounting
DThey run only when the component updates
Explain in your own words what onBeforeMount and onBeforeUnmount do in a Vue component.
Think about what happens right before a component shows or hides.
You got /3 concepts.
    Describe a real-life example where you might use onBeforeUnmount in a Vue app.
    Imagine turning off things when leaving a room.
    You got /3 concepts.