0
0
Vueframework~20 mins

Why custom directives matter in Vue - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom 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 custom directive updates an element?
Consider a Vue 3 component using a custom directive that changes the background color of an element when the directive's value changes. What will be the background color after the value updates from 'red' to 'blue'?
Vue
const app = Vue.createApp({
  data() {
    return { color: 'red' };
  },
  template: `<div v-color-bg="color">Hello</div>`
});

app.directive('color-bg', {
  beforeMount(el, binding) {
    el.style.backgroundColor = binding.value;
  },
  updated(el, binding) {
    el.style.backgroundColor = binding.value;
  }
});

const vm = app.mount('#app');

// Then later in code: vm.color = 'blue';
AThe background color will be blue after the update.
BThe background color will remain red because updated hook is not called.
CThe background color will be removed because the directive does not handle unmount.
DThe background color will be green by default.
Attempts:
2 left
💡 Hint
Think about which directive lifecycle hook runs when the bound value changes.
🧠 Conceptual
intermediate
1:30remaining
Why use custom directives instead of methods for DOM manipulation?
Which reason best explains why custom directives are preferred for direct DOM manipulation in Vue over using methods inside components?
ACustom directives run only once, methods run multiple times causing performance issues.
BMethods cannot access the DOM at all in Vue components.
CCustom directives encapsulate DOM logic and keep templates clean, while methods are for data logic.
DMethods automatically update the DOM without needing directives.
Attempts:
2 left
💡 Hint
Think about separation of concerns between DOM and data logic.
📝 Syntax
advanced
2:00remaining
Identify the correct syntax for a Vue 3 custom directive definition
Which option shows the correct way to define a Vue 3 custom directive that logs a message when the element is mounted?
A
app.directive('log', {
  mounted(el) {
    console.log('Element mounted');
  }
});
B
app.directive('log', (el) =&gt; {
  console.log('Element mounted');
});
C
app.directive('log', {
  bind(el) {
    console.log('Element mounted');
  }
});
D
app.directive('log', {
  created(el) {
    console.log('Element mounted');
  }
});
Attempts:
2 left
💡 Hint
Check Vue 3 directive lifecycle hook names.
🔧 Debug
advanced
2:00remaining
Why does this custom directive fail to update on value change?
Given this directive code, why does the element's text color not update when the bound value changes? app.directive('text-color', { beforeMount(el, binding) { el.style.color = binding.value; } });
ABecause 'beforeMount' runs after the element is removed from DOM.
BBecause the directive lacks an 'updated' hook to handle value changes.
CBecause 'binding.value' is undefined in 'beforeMount'.
DBecause the directive should use 'mounted' instead of 'beforeMount'.
Attempts:
2 left
💡 Hint
Consider which lifecycle hooks run when the value changes.
state_output
expert
2:30remaining
What is the final text content after directive and reactive state update?
In this Vue 3 example, what will be the final text content of the

element after the button is clicked? const app = Vue.createApp({ data() { return { count: 0 }; }, template: `

Initial

` }); app.directive('text-update', { mounted(el, binding) { el.textContent = `Count is ${binding.value}`; }, updated(el, binding) { el.textContent = `Count is ${binding.value}`; } }); app.mount('#app');
AThe text content will be empty after clicking the button.
B"Initial" remains because directives do not update textContent.
C"Count is 0" even after clicking the button.
D"Count is 1" after clicking the button once.
Attempts:
2 left
💡 Hint
Think about how the directive updates textContent on reactive changes.