Detailed explanation of Vue custom instructions

Detailed explanation of Vue custom instructions

Vue custom directive

Custom directives

Register a global directive v-focus, which is used to focus the element when the page is loaded

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/vue.js"></script>
	</head>
	<body>
		<div id="app">
			<input v-fo>
		</div>
		<script>
            // Register custom directive Vue.directive('fo',{
				inserted:function(el){
                    // Focus element el.focus()
				}
			})
			new Vue({
				el: '#app'
			})
		</script>
	</body>
</html>
 

image-20211112151122161

Open the interface and place the cursor directly in the input box

Hook function

The directive definition function provides several hook functions (optional).

  • bind : Called only once, when the directive is first bound to an element. This hook function can be used to define an initialization action that is executed once during binding.
  • inserted : Called when the bound element is inserted into the parent node (it can be called if the parent node exists, not necessarily in the document).
  • update : Called when the template where the bound element is located is updated, regardless of whether the bound value changes. By comparing binding values ​​before and after the update, unnecessary template updates can be ignored.
  • componentUpdated : Called when the template of the bound element completes an update cycle.
  • unbind : Called only once, when the directive is unbound from the element.

The parameters of the hook function mainly include the following

  • el : The element to which the instruction is bound, which can be used to directly manipulate the DOM.
  • binding : an object containing the following properties
  • name : The name of the directive, without the v- prefix.
  • value : The binding value of the directive, for example, v-my-directive="1+1", the value is 2.
  • oldValue : The previous value bound to the directive, only available in update and componentUpdated hooks, regardless of whether the value has changed.
  • expression : The expression or variable name of the binding value, for example, v-my-directive="1+1", the value of expression is "1+1".
  • arg : The parameter passed to the directive, for example, v-my-directive:foo, the value of arg is "foo".
  • modifiers : An object containing modifiers, for example v-my-directive.foo.bar , the value of the modifier object modifiers is {foo: true, bar: true} .
  • vnode : The virtual node generated by Vue compilation.
  • oldVnode : The previous virtual node, only available in update and componentUpdated hooks.

Output related attributes

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/vue.js"></script>
	</head>
	<body>
		<div id="app" v-hh:abc="mess">
		</div>
		<script>
			Vue.directive('hh',{
				bind: function(el,binding,vnode){
					var s = JSON.stringify
					el.innerHTML = 'name:'+s(binding.name)+'<br>'+
					'value:'+s(binding.value)+'<br>'+
					'expression:'+s(binding.expression)+'<br>'+
					'argument:'+s(binding.arg)+'<br>'+
					'modifiers:'+s(binding.modifiers)+'<br>'+
					'vnode keys:'+Object.keys(vnode).join(',')
				}
			})
			new Vue({
				el:'#app',
				data:{
					mess:'abc'
				}
			})
		</script>
	</body>
</html>
 

image-20211112163153199

Application Examples

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/vue.js"></script>
	</head>
	<body>
		<div id="app">
			<div v-hh="{text:'123',c:'blue'}"></div>
		</div>
		<script>
			Vue.directive('hh',function(el,binding){
				el.innerHTML=binding.value.text
				el.style.color=binding.value.c
			})
			new Vue({
				el:'#app'
			})
		</script>
	</body>
</html>
 

image-20211112164831459

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Use of vue global custom instructions and local custom instructions
  • Detailed code about Vue custom instructions to implement element dragging
  • How to write a custom directive for Vue3
  • Explanation and example usage of 4 custom instructions in Vue
  • vue3 custom directive details
  • Description of vue custom directives and their common hook functions

<<:  Analyze Tomcat architecture principles to architecture design

>>:  CSS to achieve compatible text alignment in different browsers

Recommend

Introduction to the functions and usage of value and name attributes in Html

1. The value used in the button refers to the text...

Writing a rock-paper-scissors game in JavaScript

This article shares the specific code for writing...

Native JavaScript carousel implementation method

This article shares the implementation method of ...

Detailed explanation of pipeline and valve in tomcat pipeline mode

Preface In a relatively complex large system, if ...

CSS layout tutorial: How to achieve vertical centering

Preface I have been summarizing my front-end know...

How to filter out certain libraries during mysql full backup

Use the --all-database parameter when performing ...

UCenter Home site adds statistics code

UCenter Home is an SNS website building system rel...

How to use nginx to block a specified interface (URL)

1. Introduction Sometimes, after the web platform...

Summarize the commonly used nth-child selectors

Preface In front-end programming, we often use th...

Sample code for configuring nginx to support https

1. Introduction Are you still leaving your websit...

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

Introduction to the use of common Dockerfile commands

Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...

HTML implements a fixed floating semi-transparent search box on mobile

Question. In the mobile shopping mall system, we ...