Vue.js Custom Directives with Object Literals
Last Updated :
12 Apr, 2022
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stage.
The Custom Directives allow the developers to create and use some custom functions easily on the elements. Like the in-built directives that are like v-model, v-for, v-if, etc., we can create our own directives which will according to what we set it. It allows reusing some logic by having access to the lower level of DOM. We can also pass data to the custom directives.
Vue.js Custom Directives with Object Literals allows us to pass multiple values to the directive as a JSON object. In the binding value, we will receive the values by their name.
Syntax: Passing the multiple values as an object literal to the directive as follows:
<div
v-decorate="{
margin: '24px',
borderRadius: '4px',
backgroundColor: 'teal',
}">
</div>
Example: In the following example, we have a directive to style a div container and to style an input. We have provided the fields which we may or may not use on your choice. This provides an easy way to apply styles to our elements and can be extended further.
Step 1: Create a new Vue.js project with the npm node.js package manager using the following command.
npm init vue@latest
Enter the project name and preset the project as follows:
Project Structure: After successful installation, the following project structure will be formed.

Project Structure
Step 3: In the script section of the App.vue file, create a custom directive and name it as decorate. In the mounted section and the updated section, add the element and the binding fields. Now modify the values accordingly. In this tutorial, we are going to modify the border-radius, colour, background color, and the placeholder text. Finally, export the directive.
App.vue
<script>
const decorate = {
mounted: (el, binding) => {
el.style.borderRadius = binding.value.borderRadius;
el.style.backgroundColor = binding.value.backgroundColor;
el.style.margin = binding.value.margin;
el.placeholder = binding.value.placeholder;
el.style.color = binding.value.color;
},
updated: (el, binding) => {
el.style.borderRadius = binding.value.borderRadius;
el.style.backgroundColor = binding.value.backgroundColor;
el.style.margin = binding.value.margin;
el.placeholder = binding.value.placeholder;
el.style.color = binding.value.color;
},
};
export default {
directives: {
decorate,
},
};
</script>
|
Step 4: In the template section of the App.vue file, add an input and div element and add the values as you wish.
App.vue
<template>
<center>
<h1 style= "text-align: center;
color: green" >
GeeksforGeeks
</h1>
<strong>
Vue.js Custom Directives with Object Literals
</strong>
<br />
</center>
<center>
<div v-decorate= "{
margin: '24px',
borderRadius: '4px',
backgroundColor: 'teal',
}" >
Welcome to GeeksforGeeks
</div>
<input type= "text"
v-decorate= "{ placeholder: 'Green Input',
color: 'green' }" />
</center>
</template>
|
Here is the full code.
App.vue
< script >
const decorate = {
mounted: (el, binding) => {
el.style.borderRadius = binding.value.borderRadius;
el.style.backgroundColor = binding.value.backgroundColor;
el.style.margin = binding.value.margin;
el.placeholder = binding.value.placeholder;
el.style.color = binding.value.color;
},
updated: (el, binding) => {
el.style.borderRadius = binding.value.borderRadius;
el.style.backgroundColor = binding.value.backgroundColor;
el.style.margin = binding.value.margin;
el.placeholder = binding.value.placeholder;
el.style.color = binding.value.color;
},
};
export default {
directives: {
decorate,
},
};
</ script >
< template >
< center >
< h1 style = "text-align: center; color: green" >GeeksforGeeks</ h1 >
< strong > Vue.js Custom Directives with Object Literals </ strong >
< br />
</ center >
< center >
< div
v-decorate="{
margin: '24px',
borderRadius: '4px',
backgroundColor: 'teal',
}"
>
Welcome to GeeksforGeeks
</ div >
< input
type = "text"
v-decorate = "{ placeholder: 'Green Input', color: 'green' }"
/>
</ center >
</ template >
|
Step 5: Run the project using the following command and see the output.
npm run dev
Output: On successfully building the project, open http://localhost:3000, and the result will be as follows.
Reference: https://vuejs.org/guide/reusability/custom-directives.html#object-literals
Similar Reads
Vue.js Custom Directives with Function Shorthand
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js Custom Directives with Components
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js v-on:click.left Directive
The v-on:click.left directive is a Vue.js directive used to add a click event listener to an element. While click directive triggers the event for all kind of clicks, this directive only triggers the event when left key of mouse is clicked. First, we will create a div element with id as app and let'
1 min read
Vue.js | v-text directive
The v-text directive is a Vue.js directive used to update a elementâs textContent with our data. It is just a nice alternative to Mustache syntax. First, we will create a div element with id as app and let's apply the v-text directive to this element with data as a message. Now we will create this m
1 min read
Vue.js v-cloak Directive
The v-cloak directive is a Vue.js directive that will remain on the element until the associated Vue instance finishes compilation. Combined with CSS rules such as [v-cloak] { display: none }, this directive can be used to hide uncompiled mustache bindings until the Vue instance is ready. First, we
1 min read
How to use model directive with two way computed property in vue.js ?
Vue.js is an open-source ModelâViewâView Model front-end JavaScript framework for building user interfaces and single-page applications. Data binding is one of the most important features in web applications. Vue.js provides two-way data binding that helps DOM to update automatically when the data M
5 min read
Vue.js v-pre Directive
The v-pre directive is a Vue.js directive used to skip compilation for this element and all its children. You can use this for displaying raw mustache tags. First, we will create a div element with id as app and let's apply the v-pre directive to an element. Further, we can use a heading element to
1 min read
Vue.js List Rendering v-for with an Object
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
3 min read
Vue.js Composition API with Templates
Vue.js is a progressive javascript framework for developing web user interfaces. It is a versatile framework providing high speed and performance. We can create Single Page Applications as well as Full Stack applications. The Composition API allows author to Vue components using imported functions r
3 min read
Vue.js | v-else directive
The v-else directive is a Vue.js directive used to toggle the display CSS property of a element only when the if condition is not satisfied. First, we will create a div element with id as app and let's apply the v-else directive to a element with data. Now we will create this data by initializing a
2 min read