Directives in Vue help you add special behavior to elements. Arguments and modifiers let you customize how these behaviors work.
Directive with arguments and modifiers in Vue
<element v-directive:argument.modifier1.modifier2="value"></element>Argument is a value after a colon (:) that the directive can use.
Modifiers are dot-separated flags that change directive behavior.
click is the argument for the v-on directive, and prevent is a modifier that stops the default click action.<button v-on:click.prevent="submitForm">Submit</button>lazy modifier makes v-model update only when the input loses focus, not on every keystroke.<input v-model.lazy="username">foo and modifiers bar and baz to customize its behavior.<div v-my-directive:foo.bar.baz="someValue"></div>This Vue component uses the v-on directive with the click argument and prevent modifier. It stops the button's default action (like page reload) and shows a message instead.
<template> <button v-on:click.prevent="showMessage">Click me</button> <p>{{ message }}</p> </template> <script setup> import { ref } from 'vue' const message = ref('') function showMessage() { message.value = 'Button clicked without page reload!' } </script>
Arguments and modifiers are optional but powerful for customizing directives.
Modifiers are always lowercase and chained with dots.
You can access arguments and modifiers inside custom directive code via binding.arg and binding.modifiers.
Directives can take arguments to specify what they act on.
Modifiers change how directives behave without extra code.
Use arguments and modifiers to make your Vue templates cleaner and more flexible.
Practice
v-bind:title uses title as an argument. What does it do?Solution
Step 1: Understand directive arguments
Directive arguments specify what part of the element the directive should affect, like an attribute or property.Step 2: Analyze
Here,v-bind:titletitleis the argument tellingv-bindto bind the value to the element'stitleattribute.Final Answer:
It tells the directive which attribute or property to target. -> Option CQuick Check:
Directive argument = target attribute/property [OK]
- Thinking arguments change directive type
- Confusing arguments with modifiers
- Assuming arguments add CSS classes
v-on:click.prevent is an example. Which is correct?Solution
Step 1: Recall Vue directive syntax
In Vue, modifiers come after the argument, separated by dots, e.g.,v-on:click.prevent.Step 2: Check each option
v-on:click.preventmatches the correct pattern: directive, colon, argument, dot, modifier. Others mix order or add parentheses incorrectly.Final Answer:
v-on:click.prevent -> Option AQuick Check:
Modifiers follow arguments with dots [OK]
- Placing modifiers before arguments
- Using parentheses with modifiers
- Switching order of argument and modifier
<button v-on:click.once.prevent="submit">Submit</button>
Solution
Step 1: Understand modifiers
.onceand.prevent.oncemakes the event handler run only once..preventcallsevent.preventDefault()to stop default browser behavior.Step 2: Analyze the directive behavior
The button's click triggerssubmitonly once and prevents default action each time.Final Answer:
The submit method runs once on click and prevents default event. -> Option DQuick Check:
.once+.prevent= run once + prevent default [OK]
- Ignoring the effect of .once modifier
- Confusing .prevent with .stop
- Assuming method runs multiple times
<input v-model.trim.lazy="username">
Solution
Step 1: Check Vue documentation for v-model modifiers
Vue allows chaining modifiers like.trimand.lazyonv-modelto trim input and update on change.Step 2: Validate syntax correctness
The syntaxv-model.trim.lazyis valid and commonly used. No argument is needed here.Final Answer:
The syntax is correct; no error present. -> Option BQuick Check:
Multiple modifiers can chain on v-model [OK]
- Thinking modifiers need commas
- Assuming arguments are required for modifiers
- Believing some modifiers can't combine
v-focus that focuses an input only once when inserted, and you want to pass an argument to specify the delay in milliseconds. Which is the correct way to use arguments and modifiers to achieve this?<input v-focus:delay.once="500">
Solution
Step 1: Understand argument and modifier roles
Arguments specify extra data like 'delay' to the directive. Modifiers like.oncechange behavior, here to run focus only once.Step 2: Analyze the directive usage
v-focus:delay.once="500"correctly uses 'delay' as argument and '.once' as modifier. The value '500' is passed as delay time.Final Answer:
Use argumentdelayfor delay and modifier.onceto focus once. -> Option AQuick Check:
Argument = data, modifier = behavior change [OK]
- Swapping argument and modifier positions
- Thinking they can't be combined
- Using modifiers as arguments
Ready to prove it?
Pass the test and this pattern moves unknown
