0
0
Vueframework~10 mins

onBeforeMount and onBeforeUnmount in Vue - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run a function before the component mounts.

Vue
<script setup>
import { onBeforeMount } from 'vue'

onBeforeMount(() => {
  console.log([1])
})
</script>
Drag options to blanks, or click blank then click option'
AbeforeMount
Bmounted
C"Component is about to mount"
DonMount
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong lifecycle hook name.
Not passing a function to onBeforeMount.
Logging a variable that is not defined.
2fill in blank
medium

Complete the code to run cleanup code before the component unmounts.

Vue
<script setup>
import { onBeforeUnmount } from 'vue'

onBeforeUnmount(() => {
  [1]('Cleaning up before unmount')
})
</script>
Drag options to blanks, or click blank then click option'
Aalert
Bfetch
CsetTimeout
Dconsole.log
Attempts:
3 left
💡 Hint
Common Mistakes
Using alert instead of console.log.
Calling a function without parentheses.
Trying to run asynchronous code without await.
3fill in blank
hard

Fix the error in the lifecycle hook import statement.

Vue
<script setup>
import { [1] } from 'vue'

[1](() => {
  console.log('Before mount')
})
</script>
Drag options to blanks, or click blank then click option'
AbeforeMount
BonBeforeMount
ConMount
Dmounted
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'beforeMount' instead of 'onBeforeMount'.
Confusing mount hooks with unmount hooks.
Misspelling the hook name.
4fill in blank
hard

Fill both blanks to log messages before mount and before unmount.

Vue
<script setup>
import { [1], [2] } from 'vue'

[1](() => console.log('Starting mount'))
[2](() => console.log('Starting unmount'))
</script>
Drag options to blanks, or click blank then click option'
AonBeforeMount
BonMounted
ConBeforeUnmount
DonUnmounted
Attempts:
3 left
💡 Hint
Common Mistakes
Using onMounted or onUnmounted instead of before hooks.
Mixing up mount and unmount hooks.
Forgetting to import both hooks.
5fill in blank
hard

Fill all three blanks to create a component that logs before mount, before unmount, and after mount.

Vue
<script setup>
import { [1], [2], [3] } from 'vue'

[1](() => console.log('Before mount'))
[3](() => console.log('Mounted'))
[2](() => console.log('Before unmount'))
</script>
Drag options to blanks, or click blank then click option'
AonBeforeMount
BonBeforeUnmount
ConMounted
DonUnmounted
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up onMounted and onBeforeMount.
Using onUnmounted instead of onBeforeUnmount.
Not importing all three hooks.