Customize a demo commandThe syntax of Vue custom directives is as follows: Vue.directive(id, definition) The two parameters passed in, id refers to the instruction ID, and definition refers to the definition object. Among them, the definition object can provide some hook functions. <div id="app"> <!-- Input box gets focus--> <input type="text" v-focus/> </div> <script> // Register a global custom directive v-focus Vue.directive("focus", { // When the bound element is inserted into the DOM. inserted(el, binding) { // Focus element el.focus(); } }) </script> <div id="app"> <p v-demo:red="msg">{{msg}}</p> </div> <script> // Global directive Vue.directive('demo', function (el, binding) { console.log(el) //p tag console.log(binding) //The output is an object obj console.log('Command name:'+binding.name) //Command nameconsole.log('Command binding value:'+binding.value) //Command binding valueconsole.log('String form of binding value:'+binding.expression) //String form of binding valueconsole.log('Parameter passed to the command:'+binding.arg) //Parameter passed to the command}) var vm = new Vue({ el: "#app", data: { msg: 'hello!' }, // Local directives:{ demo:{ inserted: function (el) { console.log(el) } } } }) </script> Object literals<div id="app"> <p v-demo="colours">{{colours.text}}</p> </div> <script> Vue.directive('demo', function (el, binding) { console.log(el) // p tag console.log(binding) // The output is an object obj console.log(binding.value) // {color: 'blue',text: 'hello!'} console.log(binding.value.color) // 'blue' console.log(binding.value.text) // 'hello!' el.style = 'color:' + binding.value.color }) var vm = new Vue({ el: "#app", data: { colours: color: 'blue', text: 'hello!' } } }) </script> Lifecycle HooksThe directive definition function provides several hook functions (optional):
<div id="app"> <p v-demo="color">{{num}}</p> <button @click="add">Add</button> <button onclick='unbind()'>Unbind</button> </div> <script> function unbind() { vm.$destroy(); //Start another method to unbind} Vue.directive('demo', { //Five hook functions for registering directives bind: function () { //1. Be bound //Prepare for binding. For example, add event listeners, or other complex operations that only need to be performed once console.log('1 - bind'); }, inserted: function () { //2. Bind to the node console.log('2 - inserted'); }, update: function () { //3. Component update//Perform corresponding updates based on the new values obtained. For the initial value, console.log('3 - update'); will also be called once. }, componentUpdated: function () { //4. Component update completed console.log('4 - componentUpdated'); }, unbind: function () { //5. Unbind//Do cleanup operations. For example, when removing the event listener bound by bind, console.log('5 - bind'); } }) var vm = new Vue({ el: "#app", data: { num: 10, color: 'red' }, methods: { add: function () { this.num++; } } }) </script> Initialize trigger methods 1 and 2, click the Add button to trigger methods 3 and 4, and click the Unbind button to trigger method 5, as shown below: This is the end of this article about the detailed explanation of Vue.js directive custom instructions. For more relevant Vue.js directive custom instructions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Steps for Docker to build a private warehouse Harbor
>>: Detailed analysis of mysql MDL metadata lock
1. Problem symptoms Version: MySQL 5.6, using the...
1. Using it with redis will cause Netty startup c...
MariaDB is installed by default in CentOS, which ...
This article example shares the specific code of ...
MySQL is a free relational database with a huge u...
This article shares the specific steps for config...
This article shares with you how to use the Vue c...
Original link https://github.com/XboxYan/no… A bu...
1. Log in to VPN using IE browser 2. Remote login...
Generic load/write methods Manually specify optio...
This article is from the Apache Spark Meetup held...
Table of contents About MariaDB database in Linux...
Effect There are currently 2 projects (project1, ...
Ubuntu does not allow root login by default, so t...
The <TH> tag is used to set the properties ...