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 is the output of this Vue custom directive usage?
Consider this Vue 3 custom directive that changes the background color of an element when it is mounted. What will be the background color of the
after the component renders?
Vue
import { createApp } from 'vue'; const app = createApp({}); app.directive('highlight', { mounted(el, binding) { el.style.backgroundColor = binding.value || 'yellow'; } }); app.component('TestComponent', { template: `<div v-highlight="'lightblue'">Hello</div>` }); app.mount('#app');
Attempts:
2 left
💡 Hint
Look at the binding.value used in the directive and the value passed in the template.
✗ Incorrect
The directive sets the background color to the value passed via v-highlight, which is 'lightblue'. If no value was passed, it would default to 'yellow'.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Vue 3 custom directive that focuses an input element when mounted?
Select the correct syntax for a Vue 3 custom directive named 'focus' that automatically focuses the element when it is inserted into the DOM.
Attempts:
2 left
💡 Hint
Check the Vue 3 directive lifecycle hooks. 'mounted' is the correct hook for when the element is inserted.
✗ Incorrect
In Vue 3, the 'mounted' hook is called when the element is inserted into the DOM. The other hooks like 'inserted' and 'bind' are from Vue 2 and do not exist in Vue 3.
🔧 Debug
advanced2:00remaining
Why does this custom directive fail to update the element's text when the bound value changes?
Given this Vue 3 custom directive, the text content of the element does not update when the bound value changes. What is the cause?
Vue
app.directive('text', { mounted(el, binding) { el.textContent = binding.value; } }); // Usage in template: <p v-text="message"></p>
Attempts:
2 left
💡 Hint
Think about what happens when the bound value changes after the element is mounted.
✗ Incorrect
The mounted hook runs only once when the element is inserted. To react to changes in the bound value, the updated hook must be used to update the element's text.
🧠 Conceptual
advanced2:00remaining
What is the purpose of the 'beforeUnmount' hook in a Vue 3 custom directive?
In Vue 3 custom directives, what does the 'beforeUnmount' hook allow you to do?
Attempts:
2 left
💡 Hint
Think about what happens just before the element disappears from the page.
✗ Incorrect
The beforeUnmount hook is called right before the element is removed from the DOM, so it is the right place to remove event listeners or free resources to avoid memory leaks.
❓ state_output
expert3:00remaining
What will be the final text content of the
element after the component updates?
Given this Vue 3 custom directive and component code, what will the
element display after the message changes from 'Hello' to 'World'?
Vue
const app = Vue.createApp({ data() { return { message: 'Hello' }; }, template: `<p v-text-update="message"></p>` }); app.directive('text-update', { mounted(el, binding) { el.textContent = binding.value; }, updated(el, binding) { el.textContent = binding.oldValue + ' ' + binding.value; } }); app.mount('#app'); // Later in code: app.message = 'World';
Attempts:
2 left
💡 Hint
Look at how the updated hook uses binding.oldValue and binding.value to set textContent.
✗ Incorrect
On mount, textContent is 'Hello'. When message changes to 'World', updated runs and sets textContent to 'Hello World' by concatenating oldValue and value.