Challenge - 5 Problems
Vue Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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?Attempts:
2 left
💡 Hint
Think about when you want to set up something once when the element appears.
✗ Incorrect
The mounted hook in Vue directives runs once when the directive is first bound to the element and inserted into the DOM. It's used to set up things like event listeners or initial styles.
❓ component_behavior
intermediate2:00remaining
When does the
updated hook of a Vue directive run?In Vue 3, what triggers the
updated hook of a custom directive?Attempts:
2 left
💡 Hint
Think about when the element's content or attributes might change after initial render.
✗ Incorrect
The updated hook runs after the containing component has updated and the element has been re-rendered. It lets you respond to changes in the element or its data.
❓ lifecycle
advanced2: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.
Attempts:
2 left
💡 Hint
Think about the lifecycle from element creation to removal.
✗ Incorrect
The mounted hook runs first when the element is inserted. Then updated runs on reactive updates. Finally, unmounted runs when the element is removed.
🔧 Debug
advanced2: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
}Attempts:
2 left
💡 Hint
Check if the hook function exists in the directive.
✗ Incorrect
If the unmounted hook is not defined, Vue has no code to run when the element is removed. Defining it is necessary for cleanup.
📝 Syntax
expert3: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.
Attempts:
2 left
💡 Hint
Remember the order of parameters in directive hooks.
✗ Incorrect
The correct order is el first, then binding. The binding object contains the value.