Simple steps to write custom instructions in Vue3.0

Simple steps to write custom instructions in Vue3.0

Preface

Vue provides a wealth of built-in directives, such as v-if, v-bind, v-on... In addition, we can also define directives through Vue.directive({}) or directives:{}

Before we start learning, we should understand the application scenarios of custom instructions. The development of any function is to solve specific problems.

Through custom instructions, we can perform more low-level operations on the DOM, which not only provides us with ideas for quick problem solving in some scenarios, but also gives us a further understanding of the underlying Vue.

first step

In main.js

The folder corresponding to the resume under src

import Directives from "@/Directives/index"; // Custom directive (@ represents src)
const app = createApp(App);
app.use(Directives);
app.mount("#app");

Step 2

import copy from "./copy"; // Import the required instructions const directivesList = {
  copy // mount};

const directives = {
  install: function (app) {
    Object.keys(directivesList).forEach((key) => {
      app.directive(key, directivesList[key]); // register });
  }
};

export default directives; // throws

Step 3

Write our directive content in copy.js Vue2 and Vue3 only modify some life cycle functions

import { ElMessage } from "element-plus";
const copy = {
  mounted (el, { value }) {
    el.$value = value;
    el.handler = () => {
      if (!el.$value) {
        // When the value is empty, give a prompt ElMessage.warning({
          message: "Hello, the copied value cannot be empty.",
          type: "warning"
        });
        return;
      }
      if (window.clipboardData) {
        window.clipboardData.setData("text", el.$value);
      } else {
        (function (content) {
          document.oncopy = function (e) {
            e.clipboardData.setData("text", content);
            e.preventDefault();
            document.oncopy = null;
          };
        })(el.$value);
        document.execCommand("Copy");
      }

      ElMessage.success("Copy successful");
    };
    // Bind click event el.addEventListener("click", el.handler);
  },
  beforeUpdate (el, {
    value
  }) {
    el.$value = value;
  },
  unmounted (el) {
    el.removeEventListener("click", el.handler);
  }
};

export default copy;

Summarize

This is the end of this article about writing custom instructions for Vue3.0. For more relevant Vue3.0 custom instructions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue3.0 custom instructions (drectives) knowledge summary
  • vue3 custom directive details
  • An article tells you how Vue3 instructions are implemented

<<:  Detailed explanation of SQL injection - security (Part 2)

>>:  Tutorial on upgrading from Centos7 to Centos8 (with pictures and text)

Recommend

Example of using supervisor to manage nginx+tomcat containers

need: Use docker to start nginx + tomcat dual pro...

MySQL query optimization: a table optimization solution for 1 million data

1. Query speed of two query engines (myIsam engin...

How to manually upgrade the node version under CentOs

1. Find the corresponding nodejs package, refer t...

Getting Started Tutorial for Beginners ⑨: How to Build a Portal Website

Moreover, an article website built with a blog pro...

Bootstrap 3.0 study notes page layout

This time we will mainly learn about layout, whic...

Detailed explanation of Vue's sync modifier

Table of contents 1. Instructions 2. Modifiers 3....

Detailed explanation of long transaction examples in MySQL

Preface: The "Getting Started with MySQL&quo...

New ways to play with CSS fonts: implementation of colored fonts

What if you designers want to use the font below ...

Introduction to /etc/my.cnf parameters in MySQL 5.7

Below are some common parameters of /etc/my.cnf o...

Nginx reverse proxy springboot jar package process analysis

The common way to deploy a springboot project to ...

CSS new feature contain controls page redrawing and rearrangement issues

Before introducing the new CSS property contain, ...

Vue implements the method of displaying percentage of echart pie chart legend

This article mainly introduces the pie chart data...

Ubuntu 20.04 sets a static IP address (including different versions)

Because Ubuntu 20.04 manages the network through ...

About the IE label LI text wrapping problem

I struggled with this for a long time, and after s...