Challenge - 5 Problems
Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the effect of the modifier in this Vue directive?
Consider the Vue directive usage: . What does the
.trim modifier do here?Vue
<template> <input v-model.trim="username" /> <p>{{ username }}</p> </template> <script setup> import { ref } from 'vue' const username = ref('') </script>
Attempts:
2 left
💡 Hint
Think about what trimming means in text processing.
✗ Incorrect
The
.trim modifier removes leading and trailing spaces from the input value before updating the bound variable.📝 Syntax
intermediate1:30remaining
Which directive syntax correctly passes an argument and a modifier?
You want to create a custom directive that accepts an argument and a modifier. Which of the following is the correct Vue directive syntax?
Attempts:
2 left
💡 Hint
Arguments come after a colon, modifiers come after a dot.
✗ Incorrect
In Vue, the argument is specified after a colon and modifiers after a dot, so
v-my-directive:arg.modifier is correct.❓ state_output
advanced2:30remaining
What is the value of the argument and modifiers inside the directive hook?
Given this directive usage:
<div v-focus:input.lazy.once>, what will be the values of binding.arg and binding.modifiers inside the directive's mounted hook?Vue
export default { mounted(el, binding) { console.log(binding.arg) console.log(binding.modifiers) } }
Attempts:
2 left
💡 Hint
Arguments come after colon, modifiers after dots.
✗ Incorrect
The argument is the string after the colon, here "input". Modifiers are the dot-separated tokens after, here lazy and once set to true.
🔧 Debug
advanced2:30remaining
Why does this custom directive not recognize the modifier?
You wrote this directive usage:
<button v-click:submit.prevent>Submit</button> but inside the directive, binding.modifiers is empty. Why?Vue
export default { mounted(el, binding) { console.log(binding.modifiers) } }
Attempts:
2 left
💡 Hint
Modifiers like .prevent work only on native event listeners.
✗ Incorrect
The .prevent modifier is a special Vue event modifier that only works on native event listeners like v-on, not on custom directives like v-click.
🧠 Conceptual
expert3:00remaining
How does Vue prioritize modifiers and arguments in directive hooks?
In a custom directive, if you use
v-example:foo.bar.baz, which of the following statements about binding.arg and binding.modifiers is true?Attempts:
2 left
💡 Hint
Remember the syntax: argument after colon, modifiers after dots.
✗ Incorrect
Vue treats the first token after colon as argument, and each dot-separated token after as a modifier set to true.