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 What is Vuex? Five properties o...
Redis uses the apline (Alps) image of Redis versi...
First, there is only one change event. changeleve...
What is element-ui element-ui is a desktop compon...
Table of contents 1. props/$emit Introduction Cod...
1. Brief introduction of the event An event is a ...
Several commonly used string methods in JavaScrip...
A composite index (also called a joint index) is ...
The CSS position attribute specifies the element&...
MySQL's CAST() and CONVERT() functions can be...
Demand background: Insert GIF dynamic images into...
mysql returns Boolean type In the first case, ret...
Table of contents 1 element offset series 1.1 Off...
background In data warehouse modeling, the origin...
1. Software Introduction VirtualBox VirtualBox is...