A directive argument is a way to pass a specific target or key to a Vue directive, written after a colon (:). For example, in v-bind:href, href is the argument specifying what to bind.
Click to reveal answer
beginner
How do modifiers change the behavior of Vue directives?
Modifiers are special suffixes added after a dot (.) in a directive to tweak its behavior. For example, v-on:click.prevent prevents the default click action.
Click to reveal answer
beginner
Example: What does v-on:submit.prevent do?
It listens for the submit event on a form but prevents the browser's default submit action, allowing you to handle it with JavaScript instead.
Click to reveal answer
intermediate
How do you access directive arguments and modifiers inside a custom directive?
Inside a custom directive's hook, you can access binding.arg for the argument and binding.modifiers for an object of modifiers (true/false).
Click to reveal answer
intermediate
Why use arguments and modifiers in Vue directives?
They let you write flexible and reusable directives by customizing what the directive targets (argument) and how it behaves (modifiers) without writing multiple directives.
Click to reveal answer
In Vue, what symbol introduces a directive argument?
A@
B.
C#
D:
✗ Incorrect
Arguments come after a colon (:), like in v-bind:src where 'src' is the argument.
What does the modifier '.stop' do in a Vue directive like v-on:click.stop?
ADelays the event
BPrevents the default browser action
CStops event propagation
DMakes the event passive
✗ Incorrect
The '.stop' modifier stops the event from bubbling up to parent elements.
How do you check if a modifier is used inside a custom directive?
Abinding.modifiers
Bbinding.value
Cbinding.expression
Dbinding.arg
✗ Incorrect
Modifiers are available as an object in binding.modifiers with keys as modifier names.
Which of these is a valid Vue directive with argument and modifier?
Av-on:click.stop
Bv-bind:href.prevent
Cv-model.lazy
Dv-if:condition
✗ Incorrect
v-on:click.stop uses an argument 'click' and a modifier 'stop'.
What is the purpose of the argument in a directive like v-bind:title?
ATo specify the event type
BTo specify the property to bind
CTo add a CSS class
DTo define a variable
✗ Incorrect
The argument 'title' tells v-bind which attribute to bind the value to.
Explain how arguments and modifiers work together in Vue directives and why they are useful.
Think about how you tell a directive what to do and how to do it.
You got /4 concepts.
Describe how you would access and use directive arguments and modifiers inside a custom Vue directive.
Look inside the binding object passed to directive hooks.
You got /4 concepts.
Practice
(1/5)
1. In Vue, what does a directive argument specify? v-bind:title uses title as an argument. What does it do?
easy
A. It adds a CSS class to the element.
B. It changes the directive into a different directive.
C. It tells the directive which attribute or property to target.
D. It defines a new event listener.
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 v-bind:title
Here, title is the argument telling v-bind to bind the value to the element's title attribute.
Final Answer:
It tells the directive which attribute or property to target. -> Option C
Hint: Arguments specify target attribute/property in directives [OK]
Common Mistakes:
Thinking arguments change directive type
Confusing arguments with modifiers
Assuming arguments add CSS classes
2. Which of the following is the correct syntax to use a modifier with a directive argument in Vue? v-on:click.prevent is an example. Which is correct?
easy
A. v-on:click.prevent
B. v-on.prevent:click
C. v-on:prevent.click
D. v-on:click.prevent()
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.prevent matches the correct pattern: directive, colon, argument, dot, modifier. Others mix order or add parentheses incorrectly.
Final Answer:
v-on:click.prevent -> Option A
Quick Check:
Modifiers follow arguments with dots [OK]
Hint: Modifiers come after arguments, separated by dots [OK]
A. The submit method runs every click and does not prevent default event.
B. The submit method runs every click and prevents default event.
C. The submit method runs once but does not prevent default event.
D. The submit method runs once on click and prevents default event.
Solution
Step 1: Understand modifiers .once and .prevent
.once makes the event handler run only once. .prevent calls event.preventDefault() to stop default browser behavior.
Step 2: Analyze the directive behavior
The button's click triggers submit only once and prevents default action each time.
Final Answer:
The submit method runs once on click and prevents default event. -> Option D
Quick Check:
.once + .prevent = run once + prevent default [OK]
Hint: Modifiers chain to combine behaviors like once and prevent [OK]
Common Mistakes:
Ignoring the effect of .once modifier
Confusing .prevent with .stop
Assuming method runs multiple times
4. Identify the error in this Vue directive usage:
<input v-model.trim.lazy="username">
medium
A. The directive should be v-model:trim.lazy with an argument.
B. The syntax is correct; no error present.
C. Modifiers .trim and .lazy cannot be used together on v-model.
D. Modifiers must be separated by commas, like .trim,.lazy.
Solution
Step 1: Check Vue documentation for v-model modifiers
Vue allows chaining modifiers like .trim and .lazy on v-model to trim input and update on change.
Step 2: Validate syntax correctness
The syntax v-model.trim.lazy is valid and commonly used. No argument is needed here.
Final Answer:
The syntax is correct; no error present. -> Option B
Quick Check:
Multiple modifiers can chain on v-model [OK]
Hint: Modifiers chain with dots, no commas needed [OK]
Common Mistakes:
Thinking modifiers need commas
Assuming arguments are required for modifiers
Believing some modifiers can't combine
5. You want to create a custom directive 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">
hard
A. Use argument delay for delay and modifier .once to focus once.
B. Use modifier .delay for delay and argument once to focus once.
C. Arguments and modifiers cannot be combined in custom directives.
D. Use v-focus.once:delay="500" to apply once and delay.
Solution
Step 1: Understand argument and modifier roles
Arguments specify extra data like 'delay' to the directive. Modifiers like .once change 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 argument delay for delay and modifier .once to focus once. -> Option A