SoFunction
Updated on 2025-04-12

How to create and use custom directives in Vue3

introduction

In modern front-end development, it is a very popular framework that provides many powerful functions to help developers build efficient user interfaces. Custom directives are an important feature that allows developers to extend the functionality of HTML elements. In Vue3, the implementation of custom directives has changed. This article will introduce in detail how to create and use custom directives in Vue3 and provide sample code to help understand.

What are custom directives?

Custom directives are a mechanism provided that allows developers to use custom directives in templates to manipulate DOM elements. The function of instructions is usually to perform certain specific operations on elements, such as adding event listening, modifying styles, etc. In Vue3, the use of custom directives becomes easier and more flexible.

Basic syntax for custom directives in Vue3

In Vue3, custom directives are defined and used differently than Vue2. Vue3 has introducedMethod to register custom instructions. Here is the basic syntax for custom directives:

const app = ({});

// Define custom commands('my-directive', {
  // Called when the bound element is inserted into the DOM  mounted(el, binding) {
    // Here you can operate on el     = ; // For example, set text color  }
});

// Create Vue instance('#app');

In the above code, we define a name calledmy-directiveCustom directives. When this directive is bound to an element,mountedThe hook will be called, and we can operate on the elements here.

Example of using custom directives

Next, we will demonstrate how to use custom directives with an example. Suppose we want to create a directive that causes the bound element to change color when the mouse is hovering.

1. Create a Vue app

First, we need to create a Vue app and register a custom directive:

const app = ({
  data() {
    return {
      hoverColor: 'blue', // The color when the mouse is hovered    };
  },
});

// Define custom commands('hover-color', {
  // When the element is mounted  mounted(el, binding) {
    const originalColor = ; // Save original color
    // Mouse hover event    ('mouseenter', () => {
       =  || 'red'; // Set hover color    });

    // Mouse departure event    ('mouseleave', () => {
       = originalColor; // Restore original color    });
  },
});

// Create Vue instance('#app');

2. Use custom commands

In HTML, we can usev-hover-colorDirectives to bind elements:

<div >
  <h1 v-hover-color="'green'">I will change color when hovering!</h1>
  <p v-hover-color>The default hover color is red。</p>
</div>

In this example, when the user hovers over a title or paragraph, the text color changes according to the binding value of the instruction.

Custom directive parameters

Custom directives can also receive parameters and modifiers. We can passbindingObjects to obtain this information. Here is an example showing how to use parameters and modifiers:

('color', {
  mounted(el, binding) {
     =  || 'black'; // Use parameters to set the color    if () {
       = 'bold'; // If there is a bold modifier, set to bold    }
  },
});

Use this directive in HTML:

<p v-color:>This text is blue and bold。</p>

In this example, we usedv-color:Instructions,blueIt's a parameter,boldIt is a modifier. Directives will style elements based on this information.

Summarize

Custom directives are a very powerful feature in Vue3 that allows developers to manipulate DOM elements in a more flexible way. Through this article, we understand how to define and use custom directives in Vue3, including basic usage, event handling, parameters and modifier use.

Custom instructions can help us encapsulate commonly used DOM operations into reusable logic, improving the maintainability and readability of our code. In actual development, the rational use of custom instructions can greatly improve development efficiency.

This is the article about the implementation of creating and using custom instructions in Vue3. For more related content on creating and using custom instructions in Vue3, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!