Vue custom directiveCustom directivesRegister 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> Open the interface and place the cursor directly in the input box Hook functionThe directive definition function provides several hook functions (optional).
The parameters of the hook function mainly include the following
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> 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> SummarizeThis 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:
|
<<: Analyze Tomcat architecture principles to architecture design
>>: CSS to achieve compatible text alignment in different browsers
1. The value used in the button refers to the text...
This article shares the specific code for writing...
This article shares the implementation method of ...
Preface In a relatively complex large system, if ...
Preface I have been summarizing my front-end know...
px(pixel) I believe everyone is familiar with the...
Use the --all-database parameter when performing ...
UCenter Home is an SNS website building system rel...
1. Introduction Sometimes, after the web platform...
Preface In front-end programming, we often use th...
Isolation of process address spaces is a notable ...
1. Introduction Are you still leaving your websit...
Introduce two methods to view MySQL user permissi...
Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...
Question. In the mobile shopping mall system, we ...