VUE Learning Path (2) - - Vue.directive of Global API

Keywords: Vue IE Javascript

One. Global API:

APIA global is not in the constructor, but declares global variables first or defines some new functions directly on VUE. VUE has built-in global APII, such as Vue.directive. Simply put, we define new functions with API functions provided by Vue outside the constructor.

Two. Vue.directive

1. Let's make a small example with custom designation to make the number green when initializing.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>vue.directive Custom directives</title>
    <script type="text/javascript" src="https://unpkg.com/vue@2.5.16/dist/vue.js" ></script>
</head>
<body>
    <h1>vue.directive Custom directives</h1>
    <hr>
    <div id="app">
        <div v-jspang='color'>{{ num }}</div>
        <p><button @click="add">ADD</button></p>
    </div>

    <p>
        <button onclick ="unbind()">Untying</button>
    </p>

    <script>
        //The following function is added to observe the fifth phase of the life cycle of custom instructions in the console
        function unbind(){
            app.$destroyed();
        }
        Vue.directive("jspang",{
            bind:function(el,binding){//Bound
                // console.log(el);
                // console.log(binding);
                // console.log(binding.name);
                // console.log(binding.expression);
                console.log('1 - bind');
                el.style="color:"+binding.value;
            },
            inserted:function(){//Binding to nodes
                console.log('2 - inserted');
            },
            update:function(){//Component update
                console.log('3 - update');
            },
            componentUpdated:function(){//Component Update Completed
                console.log('4 - componentUpdated');
            },
            unbind:function(){//Unbind
                console.log('5 - unbind');
            }
        });


        var app = new Vue({
            el : '#app',
            data : { 
                num : 10,
                color : 'red'
            },
            methods:{
                add:function(){
                    this.num++;
                }
            }
        })
    </script>
</body>
</html>

2. Three parameters passed in the custom instruction:

el: Elements bound by instructions that can be used to manipulate DOM directly

binding: An object that contains a lot of information about instructions.

Vnode: Virtual node generated by Vue compilation

3. Life cycle of custom functions:

            bind:function(){//Bound
                console.log('1 - bind');
            },
            inserted:function(){//Binding to nodes
                console.log('2 - inserted');
            },
            update:function(){//Component update
                console.log('3 - update');
            },
            componentUpdated:function(){//Component Update Completed
                console.log('4 - componentUpdated');
            },
            unbind:function(){//Unbind
                console.log('5 - unbind');
            }




//In specific examples, write
Vue.directive("jspang",{
            bind:function(){//Bound
                console.log('1 - bind');
            },
            inserted:function(){//Binding to nodes
                console.log('2 - inserted');
            },
            update:function(){//Component update
                console.log('3 - update');
            },
            componentUpdated:function(){//Component Update Completed
                console.log('4 - componentUpdated');
            },
            unbind:function(){//Unbind
                console.log('5 - unbind');
            }
        });
//Can be a specific stage function, add statements to achieve a specific effect. Like the green number on the article.

 

Posted by silviuchingaru on Mon, 04 Feb 2019 06:03:16 -0800