1. Registering custom instructionsThe following examples are all custom instructions for implementing an input box to automatically gain focus. 1.1. Global custom instructions In vue2, global custom directives are mounted on the Vue object through Example 1 : Vue2 global custom instructions Vue.directive('focus',{ inserted:(el)=>{ el.focus() } }) In Example 2 : Vue3 global custom instructions //Global custom directive app.directive('focus',{ mounted(el){ el.focus() } }) //Component uses <input type="text" v-focus /> 1.2. Local custom instructions In a component, Example 3 : Local custom instructions <script> //Local custom instructions const defineDir = { focus: mounted(el){ el.focus() } } } export default { directives:defineDir, setup(){} } </script> 2. Lifecycle hook functions in custom instructionsA directive definition object can provide the following hook functions (all optional and introduced as needed)
Example 3 : Test the execution of lifecycle functions within instructions <template> <div> <input type="text" v-focus v-if="show"><br> <button @click="changStatus">{{show?'Hide':'Show'}}</button> </div> </template> //Local custom instructions const autoFocus = { focus: created(){ console.log('created'); }, beforeMount(){ console.log('beforeMount'); }, mounted(el){ console.log('mounted'); }, beforeUpdated(){ console.log('beforeUpdated') }, updated(){ console.log('updated'); }, beforeUnmount(){ console.log('beforeUnmount'); }, unmounted(){ console.log('unmounted'); } }, } import { ref } from 'vue' export default { directives:autoFocus, setup(){ const show = ref(true) return { show, changStatus(){ show.value = !show.value } } } } By clicking the button, we find that when an When However, At this point, if we change From vue2 to vue3, the lifecycle hook function of custom instructions has changed. The specific changes are as follows:
3. Parameters of custom instruction hook functionThe hook function is given the following parameters:
The properties contained in binding are:
<template> <div> <div v-fixed >positioning</div> </div> </template> <script> //Custom instruction dynamic parameters const autoFocus = { fixed: beforeMount(el,binding){ console.log('el',el) console.log('binding',binding) } } } export default { directives:autoFocus, setup(){ } } </script> 4. Custom command parametersCustom instructions can also have parameters, which can be dynamic and updated in real time based on component instance data. Example 4 : Customizing dynamic parameters of instructions <template> <div> <div v-fixed:pos="posData" style="width:100px;height:100px;background:grey">Positioning</div> </div> </template> <script> //Custom instruction dynamic parameters const autoFocus = { fixed: beforeMount(el,binding){ el.style.position = "fixed" el.style.left = binding.value.left+'px' el.style.top = binding.value.top + 'px' } } } export default { directives:autoFocus, setup(){ const posData = { left:20, top:200 } return { posData, } } } </script> When do you need custom directives?
This is the end of this article about the details of vue3 custom instructions. For more relevant vue3 custom instructions, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The role and methods of information communication in website visual design (picture and text)
>>: Summary of pitfalls in virtualbox centos7 nat+host-only networking
Table of Contents Introduction Synchronous Asynch...
Preface: I recently started to study the construc...
The installation tutorial of mysql 5.7.19 winx64 ...
First, let me explain the application method. The...
Case 1: Last submission and no push Execute the f...
The MySQL development team officially released th...
1. Install MySQL # Download mysql in docker docke...
Table of contents 1. Configure bridging and captu...
Table of contents 1. Basic environment configurat...
<br />The frame structure allows several web...
mysql returns Boolean type In the first case, ret...
<textarea></textarea> is used to crea...
I started learning MySQL recently. The installati...
The overall architecture of MySQL is divided into...
HTML5 is the next version of the HTML standard. M...