0
0
Vueframework~20 mins

Directive with arguments and modifiers in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
AIt disables the input field so the user cannot type.
BIt prevents the input from updating the model until the input loses focus.
CIt automatically removes whitespace from both ends of the input value before updating the model.
DIt converts the input value to uppercase before updating the model.
Attempts:
2 left
💡 Hint
Think about what trimming means in text processing.
📝 Syntax
intermediate
1: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?
Av-my-directive[arg].modifier
Bv-my-directive:arg.modifier
Cv-my-directive.modifier:arg
Dv-my-directive:modifier.arg
Attempts:
2 left
💡 Hint
Arguments come after a colon, modifiers come after a dot.
state_output
advanced
2: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)
  }
}
A"input" and { lazy: true, once: true }
B"focus" and { input: true, lazy: true, once: true }
Cundefined and {}
D"input.lazy.once" and {}
Attempts:
2 left
💡 Hint
Arguments come after colon, modifiers after dots.
🔧 Debug
advanced
2: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)
  }
}
ABecause the directive name is 'click' but the modifier 'prevent' is only recognized on native events, not custom directives.
BBecause the argument 'submit' is invalid and blocks modifiers from being parsed.
CBecause the directive is missing a value, so modifiers are ignored.
DBecause modifiers must be declared in the directive definition to be recognized.
Attempts:
2 left
💡 Hint
Modifiers like .prevent work only on native event listeners.
🧠 Conceptual
expert
3: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?
A<code>binding.arg</code> is undefined, and <code>binding.modifiers</code> is { foo: true, bar: true, baz: true }
B<code>binding.arg</code> is 'foo.bar', and <code>binding.modifiers</code> is { baz: true }
C<code>binding.arg</code> is 'foo.bar.baz', and <code>binding.modifiers</code> is {}
D<code>binding.arg</code> is 'foo', and <code>binding.modifiers</code> is { bar: true, baz: true }
Attempts:
2 left
💡 Hint
Remember the syntax: argument after colon, modifiers after dots.