1. 程式人生 > >vue自定義指令的建立與使用

vue自定義指令的建立與使用

一、自定義指令的建立和使用

Vue自帶的指令很多,v-for/v-if/v-else/v-else-if/v-model/v-bind/v-on/v-show/v-html/v-text... 但是這些指令都是比較偏向於工具化,有些時候在實現具體的業務邏輯的時候,發現不夠用,如何來自定義指令.

1、自定義指令 ①建立 new Vue({   directives:{     change:{       bind:function(){},       update:function(){},       unbind:function(){}     }   } })

在自定義指令時,在指令對應的配置物件中有3個處理函式指令對應的操作 bind   指令在繫結到元素要執行的操作 update   如果在呼叫指令時候,傳了引數,當引數變化時候,就會執行update所指定的方法 unbind   解綁要執行的操作

②使用自定義指令 directives:{   hello:{     bind:function(){},     update:function(){},     unbind:function(){}   } }

使用:   v-hello

注意事項:建議在給指令的命名採用小駝峰式的命名方式,比如changeBackgroundColor,在使用的時候,採用烤串式寫法 v-change-background-color

(方法:引數,返回值)

bind方法以及update方法 都是有引數的, 一個是el,對應的是呼叫指令的元素 一個bindings,是一個物件:name/rawName/value/oldValue...

複製程式碼

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="js/vue.js"></script>
    <title></title>
</head>
<body>

<div id="container">
    <p>{{msg}}</p>
    <!-- 準備實現需求:
    在h1標籤上面,加上一個按鈕,當點選按鈕時候,對count實現一次
    自增操作,當count等於5的時候,在控制檯輸出‘it is a test’
    -->
    <button @click="handleClick">clickMe</button>
    <h1
            v-if="count < 6"
            v-change="count">it is a custom directive</h1>
</div>

<script>
    //directive
    new Vue({
        el: '#container',
        data: {
            msg: 'Hello Vue',
            count:0
        },
        methods:{
            handleClick: function () {
                //按鈕單擊,count自增
                this.count++;
            }
        },
        directives:{
            change:{
                bind: function (el,bindings) {
                    console.log('指令已經繫結到元素了');
                    console.log(el);
                    console.log(bindings);
                    //準備將傳遞來的引數
                    // 顯示在呼叫該指令的元素的innerHTML
                    el.innerHTML = bindings.value;
                },
                update: function (el,bindings) {
                    console.log('指令的資料有所變化');
                    console.log(el);
                    console.log(bindings);
                    el.innerHTML = bindings.value;
                    if(bindings.value == 5)
                    {
                        console.log(' it is a test');
                    }
                },
                unbind: function () {
                    console.log('解除綁定了');
                }
            }
        }
    })
</script>

</body>
</html>

複製程式碼

複製程式碼

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="js/vue.js"></script>
    <title></title>
</head>
<body>

<div id="container">
    <p>{{msg}}</p>
    <h1 v-change-background-color="myBgColor">
        it is a header1
    </h1>
</div>

<script>
    new Vue({
        el: '#container',
        data: {
            msg: 'Hello Vue',
            myBgColor:'#ff0000'
        },
        directives:{
            changeBackgroundColor:{
                bind: function (el,bindings) {
                    console.log('in bind ');
                    console.log(bindings.value);
                    el.style.backgroundColor = bindings.value;
                }
            }
        }
    })
</script>

</body>
</html>

複製程式碼

 <h4 v-change-background-color="'red'">背景色</h4>這樣也是可以的,但是寫死了,不好