PrefaceVue 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 stepIn 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 2import 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 3Write 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; SummarizeThis 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:
|
<<: Detailed explanation of SQL injection - security (Part 2)
>>: Tutorial on upgrading from Centos7 to Centos8 (with pictures and text)
Share a real-time clock effect implemented with n...
1. Construction components 1. A form must contain...
Innodb includes the following components 1. innod...
location matching order 1. "=" prefix i...
Table of contents 1. Initial SQL Preparation 2. M...
I usually like to visit the special pages or prod...
Table of contents Overview 1. RangeError 2. Refer...
Possible reasons: The main reason why Seata does ...
1. Simulate database data 1-1 Create database and...
Table of contents need: Ideas: lesson: Share the ...
This article example shares the specific code of ...
Without further ado, here is a demo picture. The ...
Linux: Linux version 3.10.0-123.9.3.el7.x86_64 Ng...
How to install PHP7 on Linux? 1. Install dependen...
If we want to make a carousel, we must first unde...