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

Native JS to implement real-time clock

Share a real-time clock effect implemented with n...

How to use Antd's Form component in React to implement form functions

1. Construction components 1. A form must contain...

Summary of important components of MySQL InnoDB

Innodb includes the following components 1. innod...

Summary of nginx configuration location method

location matching order 1. "=" prefix i...

How to view the execution time of SQL statements in MySQL

Table of contents 1. Initial SQL Preparation 2. M...

CSS3 realizes bouncing ball animation

I usually like to visit the special pages or prod...

7 native JS error types you should know

Table of contents Overview 1. RangeError 2. Refer...

How to solve the problem that Seata cannot use MySQL 8 version

Possible reasons: The main reason why Seata does ...

Implementation of MySQL index-based stress testing

1. Simulate database data 1-1 Create database and...

Vue dynamic menu, dynamic route loading and refresh pitfalls

Table of contents need: Ideas: lesson: Share the ...

js object to achieve data paging effect

This article example shares the specific code of ...

Nginx server https configuration method example

Linux: Linux version 3.10.0-123.9.3.el7.x86_64 Ng...

Detailed explanation of how to install PHP7 on Linux

How to install PHP7 on Linux? 1. Install dependen...

JavaScript Dom implements the principle and example of carousel

If we want to make a carousel, we must first unde...