Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
Custom directives in Vue are a way to reuse code that manipulates the DOM. Vue 3.0 uses a slightly different syntax for custom directives than Vue 2, so be sure you're looking at the correct version of the documentation.
Let's create a custom directive that changes the background color of an element.
Step 1: Create a Vue project.
If you haven't created your Vue 3 project yet, you can do so using the Vue CLI.
vue create my-project
Choose Vue 3 when asked which version of Vue to use.
Step 2: In your main.js
(or main.ts
if using TypeScript) file, you can define a global custom directive like this:
import { createApp } from 'vue' import App from './App.vue' const app = createApp(App) app.directive('highlight', { beforeMount(el, binding, vnode, prevVnode) { el.style.backgroundColor = binding.value; } }) app.mount('#app')
This creates a directive named highlight
. This directive changes the background color of the HTML element it is used on, to the color provided as its value. The beforeMount
hook is called right before the directive's element is mounted to the DOM.
Step 3: Use your directive in a component.
You can then use your custom directive in any of your Vue components. Here's how you might use it in your App.vue
file:
<template> <div v-highlight="'lightgreen'"> This should have a light green background. </div> </template> <script> export default { name: 'App', }; </script>
This will apply the highlight
directive to the div, resulting in the div having a light green background.
Step 4: Run your application.
You can run your Vue 3 application using the following command:
npm run serve
And there you go! You've created and used a custom directive in Vue 3.0. This is a fairly simple example, but you can create more complex directives as needed by your application.
Creating Custom Directives in Vue 3.0:
// customDirective.js export const customDirective = { mounted(el, binding) { // Directive logic when element is mounted el.style.color = binding.value; }, };
Registering Custom Directives in Vue 3.0:
app.directive
method.// main.js import { createApp } from 'vue'; import { customDirective } from './customDirective'; const app = createApp(App); app.directive('my-directive', customDirective);
Vue 3.0 Custom Directive Example:
<!-- MyComponent.vue --> <template> <div v-my-directive="'red'">Colored Text</div> </template> <script> export default { // Component logic }; </script>
Modifiers for Custom Directives in Vue 3.0:
<!-- MyComponent.vue --> <template> <input v-my-directive.trim="someValue"> </template>
Vue 3.0 Directive Arguments:
<!-- MyComponent.vue --> <template> <div v-my-directive:argName="value">Dynamic Argument</div> </template>
Scoped Custom Directives in Vue 3.0:
v-my-directive
syntax.<!-- MyComponent.vue --> <template> <div v-my-directive="value">Scoped Directive</div> </template>
Dynamic Custom Directives in Vue 3.0:
<!-- MyComponent.vue --> <template> <div v-my-directive="condition">Dynamic Directive</div> </template>
Vue 3.0 Custom Directive and Reactivity:
// customDirective.js export const customDirective = { updated(el, binding) { el.innerHTML = binding.value; }, };