📖 Scenario: You are building a Vue 3 app where you want to create a custom directive that changes the background color of an element when it is added to the page, updates the color when the bound value changes, and cleans up when the element is removed.
🎯 Goal: Create a custom Vue directive called v-highlight that uses the mounted, updated, and unmounted hooks to manage the background color of an element.
📋 What You'll Learn
Create a Vue 3 app with a custom directive named highlight
Use the mounted hook to set the element's background color to the directive's value
Use the updated hook to update the background color when the value changes
Use the unmounted hook to reset the background color when the element is removed
💡 Why This Matters
🌍 Real World
Custom directives are useful when you want to add reusable behavior to HTML elements in Vue apps, like animations, event handling, or style changes.
💼 Career
Understanding directive hooks helps you build maintainable Vue components and extend Vue's functionality, a valuable skill for frontend developer roles.
Progress0 / 4 steps
1
Create the Vue app and initial directive setup
Create a Vue 3 app using createApp and define a custom directive called highlight with an empty object as its definition.
Vue
Hint
Start by importing createApp from Vue and create an app instance. Then add a directive named highlight with an empty object.
2
Add the mounted hook to set background color
Inside the highlight directive, add the mounted hook that sets the element's background color to the directive's value using el.style.backgroundColor = binding.value.
Vue
Hint
The mounted hook runs when the element is added to the DOM. Use binding.value to get the color value.
3
Add the updated hook to change background color on value change
Add the updated hook inside the highlight directive that updates the element's background color to the new binding.value when the directive's value changes.
Vue
Hint
The updated hook runs when the directive's value changes. Update the background color similarly to mounted.
4
Add the unmounted hook to reset background color
Add the unmounted hook inside the highlight directive that resets the element's background color to an empty string '' when the element is removed from the DOM.
Vue
Hint
The unmounted hook runs when the element is removed. Reset the background color to remove the highlight.
Practice
(1/5)
1. What does the mounted hook in a Vue directive do?
easy
A. Runs every time the component updates
B. Runs once when the directive is attached to the element
C. Runs when the directive is removed from the element
D. Runs only when the element is clicked
Solution
Step 1: Understand the purpose of mounted hook
The mounted hook runs once when the directive is first attached to the element in the DOM.
Step 2: Compare with other hooks
The updated hook runs on data or element changes, and unmounted runs when the directive is removed, so they do not match the description of mounted.
Final Answer:
Runs once when the directive is attached to the element -> Option B
Quick Check:
mounted = runs once on attach [OK]
Hint: Remember: mounted runs once when directive starts [OK]
Common Mistakes:
Confusing mounted with updated hook
Thinking mounted runs multiple times
Mixing unmounted with mounted
2. Which of the following is the correct syntax to define the unmounted hook in a Vue directive?
easy
A. onUnmounted(el, binding) { /* code */ }
B. unmount(el, binding) { /* code */ }
C. destroyed(el, binding) { /* code */ }
D. unmounted(el, binding) { /* code */ }
Solution
Step 1: Recall the correct hook name
The correct hook name for cleanup when a directive is removed is unmounted.
Step 2: Check syntax correctness
The hook is defined as a function with parameters el and binding, so unmounted(el, binding) { /* code */ } is correct.
Final Answer:
unmounted(el, binding) { /* code */ } -> Option D
Quick Check:
Correct hook name is unmounted [OK]
Hint: Use exact hook names: unmounted, not unmount or destroyed [OK]
Common Mistakes:
Using 'unmount' instead of 'unmounted'
Confusing with lifecycle hooks like 'destroyed'
Adding 'on' prefix incorrectly
3. Given this directive code, what will be logged when the bound element's text changes?
Directive hooks receive the element as the first parameter. The updated hook is missing the el parameter.
Step 2: Verify other hooks
mounted and unmounted correctly have el parameter. The unmounted hook is correctly named.
Final Answer:
The updated hook is missing the 'el' parameter -> Option C
Quick Check:
All hooks need 'el' parameter [OK]
Hint: All directive hooks need 'el' parameter [OK]
Common Mistakes:
Omitting parameters in hooks
Confusing lifecycle hook names
Assuming no parameters needed
5. You want to create a directive that adds a click event listener when attached and removes it when the directive is removed. Which hooks should you use to add and remove the listener?
hard
A. Add listener in mounted, remove in unmounted
B. Add listener in updated, remove in mounted
C. Add listener in unmounted, remove in updated
D. Add listener in updated, remove in updated
Solution
Step 1: Determine when to add event listener
The listener should be added once when the directive attaches, so use mounted.
Step 2: Determine when to remove event listener
Cleanup should happen when the directive is removed, so use unmounted to remove the listener.
Final Answer:
Add listener in mounted, remove in unmounted -> Option A
Quick Check:
mounted = add, unmounted = cleanup [OK]
Hint: Add in mounted, clean up in unmounted [OK]
Common Mistakes:
Adding/removing listeners in updated hook
Removing listener before adding it
Using unmounted to add listener
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.