SOURCE

console 命令行工具 X clear

                    
>
console
<!DOCTYPE html>
<html lang="en">

<head>
	<title>Vue.directive 自定义指令</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js">
	</script>
</head>

<body>
	<h1>Vue.directive 自定义指令</h1>
	<hr>
	<div id="app">
		<div v-jsdirective="color">{{num}}</div>
		<p><button @click="add">Add</button></p>
    </div>

    <p><button onclick="unbind()">解绑</button></p>



    <script type="text/javascript">
        function unbind(params) {
            app.$destroy();
        }

        // jsdirective标识v-jsdirective会自动省略v-
        // el表示所在元素<div>10</div>
        // binding是一个对象,包含元素未处理前的很多值
        Vue.directive("jsdirective", {
            bind: function (el, binding) { //被绑定
                console.log('1 - bind');

                console.log(el);
                console.log(binding);
                console.log(binding.name);
                console.log(binding.rawName);
                console.log(binding.value);
                console.log(binding.expression);

                el.style = "color:" + binding.value;
            },
            inserted: function () { //绑定到节点
                console.log('2 - inserted');
            },
            update: function () { //组件更新
                console.log('3 - update');
            },
            componentUpdated: function () { //组件更新完成
                console.log('4 - componentUpdated');
            },
            unbind: function () { //解绑
                console.log('5 - unbind');
            }
        })


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

</html>