0
0
Vueframework~20 mins

Directive hooks (mounted, updated, unmounted) in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Vue directive's mounted hook runs?
Consider a custom directive in Vue 3 using the mounted hook. What is the main purpose of this hook?
AIt runs when the directive is removed from the element, to clean up resources.
BIt runs every time the component updates, to refresh the directive's behavior.
CIt runs once when the directive is first attached to the element, allowing setup like event listeners.
DIt runs before the element is inserted into the DOM.
Attempts:
2 left
💡 Hint
Think about when you want to set up something once when the element appears.
component_behavior
intermediate
2:00remaining
When does the updated hook of a Vue directive run?
In Vue 3, what triggers the updated hook of a custom directive?
AEvery time the component's reactive data changes and the element is re-rendered.
BOnly when the directive is unbound from the element.
CWhen the directive is first bound to the element.
DBefore the element is inserted into the DOM.
Attempts:
2 left
💡 Hint
Think about when the element's content or attributes might change after initial render.
lifecycle
advanced
2:30remaining
What is the correct order of Vue directive hooks when an element is inserted, updated, and then removed?
Arrange the Vue directive hooks in the order they are called during the element's lifecycle: insertion, update, removal.
A2,1,3
B1,2,3
C3,1,2
D1,3,2
Attempts:
2 left
💡 Hint
Think about the lifecycle from element creation to removal.
🔧 Debug
advanced
2:30remaining
Why does this Vue directive's unmounted hook not run?
Given this directive code, the unmounted hook never triggers. What is the likely cause?
export default {
  mounted(el) { console.log('mounted'); },
  updated(el) { console.log('updated'); },
  // unmounted hook missing
}
ABecause the directive must use <code>beforeUnmount</code> instead.
BBecause the <code>updated</code> hook prevents <code>unmounted</code> from running.
CBecause the element is never removed from the DOM.
DBecause the <code>unmounted</code> hook is not defined in the directive object.
Attempts:
2 left
💡 Hint
Check if the hook function exists in the directive.
📝 Syntax
expert
3:00remaining
Which Vue directive hook signature is correct for accessing the element and binding value?
Select the correct syntax for a Vue 3 directive hook that accesses the element and the binding value.
Amounted(el, binding) { console.log(binding.value); }
Bmounted(binding, el) { console.log(binding.value); }
Cmounted(el) { console.log(el.binding.value); }
Dmounted(el, binding, vnode) { console.log(vnode.value); }
Attempts:
2 left
💡 Hint
Remember the order of parameters in directive hooks.