Vue.js course ยท Module 14: Directives & Plugins
Directive Hooks - Arguments, Modifiers, Values
In this lesson6
In the NOVA LAB Command Center, every system requires precise parameters. Vue directives offer a rich set of data through the binding object.
The Binding Object
Each directive hook receives two main parameters: el (the DOM element) and binding (an object with data):
app.directive('demo', {
mounted(el, binding) {
// binding.value - the passed value: v-demo="123" => 123
// binding.arg - the argument: v-demo:color => 'color'
// binding.modifiers - modifiers: v-demo.bold => { bold: true }
// binding.oldValue - the previous value (in updated)
}
})Directive Value: binding.value
The value is the JavaScript expression passed after =:
<template>
<div v-color="'#00ff88'">Green text</div>
<div v-color="themeColor">Dynamic color</div>
<div v-color="{ bg: '#0a0e27', text: '#00ff88' }">Object</div>
</template>app.directive('color', (el, binding) => {
if (typeof binding.value === 'string') {
el.style.color = binding.value
} else {
el.style.backgroundColor = binding.value.bg
el.style.color = binding.value.text
}
})Arguments: binding.arg
The argument is the part after the colon - it lets you specify what the directive should modify:
<template>
<div v-style:color="'#00ff88'">Text color</div>
<div v-style:backgroundColor="'#0a0e27'">Background color</div>
<div v-style:fontSize="'20px'">Font size</div>
</template>app.directive('style', (el, binding) => {
if (binding.arg) {
el.style[binding.arg] = binding.value
}
})Dynamic Arguments
The argument can be dynamic - use square brackets:
<script setup>
import { ref } from 'vue'
const prop = ref('color')
</script>
<template>
<div v-style:[prop]="'#00ff88'">Dynamic attribute</div>
</template>Modifiers: binding.modifiers
Modifiers are boolean flags after a dot:
<template>
<p v-format.bold.uppercase>nova lab station</p>
</template>app.directive('format', (el, binding) => {
if (binding.modifiers.bold) {
el.style.fontWeight = 'bold'
}
if (binding.modifiers.uppercase) {
el.textContent = el.textContent.toUpperCase()
}
if (binding.modifiers.italic) {
el.style.fontStyle = 'italic'
}
})Putting It All Together
Full directive syntax:
v-name:argument.modifier1.modifier2="value"Example:
<div v-tooltip:top.slow="'Station information'">
Hover over me
</div>Where:
tooltip- directive nametop- argument (position)slow- modifier (animation)'Station information'- value (text)
Code for this lesson: App.vue
<script setup>
import { ref } from 'vue'
// Directive with binding.arg
const vStyle = (el, binding) => {
if (binding.arg) {
el.style[binding.arg] = binding.value
}
}
// Directive with binding.modifiers
const vFormat = (el, binding) => {
let text = el.textContent
if (binding.modifiers.uppercase) {
text = text.toUpperCase()
}
if (binding.modifiers.lowercase) {
text = text.toLowerCase()
}
el.textContent = text
if (binding.modifiers.bold) {
el.style.fontWeight = 'bold'
}
if (binding.modifiers.italic) {
el.style.fontStyle = 'italic'
}
}
// Directive combining everything
const vPanel = {
mounted(el, binding) {
const color = binding.value || '#00ff88'
const size = binding.arg || 'md'
const paddings = { sm: '8px', md: '16px', lg: '24px' }
el.style.border = '2px solid ' + color
el.style.padding = paddings[size] || paddings.md
el.style.borderRadius = '8px'
if (binding.modifiers.glow) {
el.style.boxShadow = '0 0 15px ' + color + '40'
}
if (binding.modifiers.dashed) {
el.style.borderStyle = 'dashed'
}
}
}
const dynamicProp = ref('color')
</script>
<template>
<div class="nova-lab">
<h1>Directive Hooks - Args & Modifiers</h1>
<section class="section">
<h2>binding.arg (v-style:property)</h2>
<p v-style:color="'#00ff88'">Green color</p>
<p v-style:fontSize="'20px'">Larger font</p>
<p v-style:backgroundColor="'rgba(0, 180, 216, 0.2)'">Cyan background</p>
</section>
<section class="section">
<h2>binding.modifiers (v-format.bold.uppercase)</h2>
<p v-format.uppercase>nova lab station</p>
<p v-format.bold.italic>important message</p>
<p v-format.uppercase.bold>red alert</p>
</section>
<section class="section">
<h2>All together: v-panel</h2>
<div v-panel:md.glow="'#00ff88'" style="margin-bottom: 10px">
<p>v-panel:md.glow="#00ff88"</p>
</div>
<div v-panel:lg.dashed="'#00b4d8'" style="margin-bottom: 10px">
<p>v-panel:lg.dashed="#00b4d8"</p>
</div>
<div v-panel:sm.glow.dashed="'#e74c3c'">
<p>v-panel:sm.glow.dashed="#e74c3c"</p>
</div>
</section>
<section class="section">
<h2>Dynamic argument: v-style:[prop]</h2>
<select v-model="dynamicProp" class="select">
<option value="color">color</option>
<option value="fontSize">fontSize</option>
<option value="backgroundColor">backgroundColor</option>
</select>
<p v-style:[dynamicProp]="dynamicProp === 'fontSize' ? '22px' : dynamicProp === 'backgroundColor' ? 'rgba(0,255,136,0.2)' : '#00b4d8'" style="margin-top: 8px">
Text changes dynamically
</p>
</section>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #c0c0c0;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; margin-bottom: 20px; }
h2 { color: #00b4d8; font-size: 16px; margin-bottom: 10px; }
.section {
background: rgba(0, 180, 216, 0.05);
border: 1px solid rgba(0, 180, 216, 0.2);
padding: 15px;
border-radius: 8px;
margin-bottom: 15px;
}
.select {
background: #0a0e27;
color: #00ff88;
border: 1px solid #00ff88;
padding: 8px;
border-radius: 4px;
font-family: monospace;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What does binding.value contain in the directive v-demo="123"?
2. What does binding.arg return in the directive v-style:color="'red'"?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Use binding.arg as the CSS property name and binding.value as the value.
- Horizontal ordering
Arrange the correct syntax of a directive with an argument and value:
- Code editor
Check binding.modifiers.bold, binding.modifiers.uppercase, and binding.modifiers.italic.