溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue自定義指令的創(chuàng)建和使用方法實(shí)例分析

發(fā)布時(shí)間:2020-09-13 14:21:42 來源:腳本之家 閱讀:173 作者:匿名的girl 欄目:web開發(fā)

本文實(shí)例講述了vue自定義指令的創(chuàng)建和使用方法。分享給大家供大家參考,具體如下:

一、自定義指令的創(chuàng)建和使用

Vue自帶的指令很多,v-for/v-if/v-else/v-else-if/v-model/v-bind/v-on/v-show/v-html/v-text...
但是這些指令都是比較偏向于工具化,有些時(shí)候在實(shí)現(xiàn)具體的業(yè)務(wù)邏輯的時(shí)候,發(fā)現(xiàn)不夠用,如何來自定義指令.

1、自定義指令

① 創(chuàng)建

new Vue({
  directives:{
    change:{
      bind:function(){},
      update:function(){},
      unbind:function(){}
    }
  }
})

在自定義指令時(shí),在指令對(duì)應(yīng)的配置對(duì)象中有3個(gè)處理函數(shù)指令對(duì)應(yīng)的操作

bind
  指令在綁定到元素要執(zhí)行的操作
update
  如果在調(diào)用指令時(shí)候,傳了參數(shù),當(dāng)參數(shù)變化時(shí)候,就會(huì)執(zhí)行update所指定的方法
unbind
  解綁要執(zhí)行的操作

② 使用自定義指令

directives:{
  hello:{
    bind:function(){},
    update:function(){},
    unbind:function(){}
  }
}

使用:

v-hello

注意事項(xiàng):

建議在給指令的命名采用小駝峰式的命名方式,比如changeBackgroundColor,在使用的時(shí)候,采用烤串式寫法 v-change-background-color

(方法:參數(shù),返回值)

bind方法以及update方法 都是有參數(shù)的,
一個(gè)是el,對(duì)應(yīng)的是調(diào)用指令的元素
一個(gè)bindings,是一個(gè)對(duì)象:name/rawName/value/oldValue...

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <script src="https://cdn.bootcss.com/vue/2.0.1/vue.min.js"></script>
  <title>www.jb51.net vue自定義指令</title>
</head>
<body>
<div id="container">
  <p>{{msg}}</p>
  <!-- 準(zhǔn)備實(shí)現(xiàn)需求:
  在h2標(biāo)簽上面,加上一個(gè)按鈕,當(dāng)點(diǎn)擊按鈕時(shí)候,對(duì)count實(shí)現(xiàn)一次
  自增操作,當(dāng)count等于5的時(shí)候,在控制臺(tái)輸出‘it is a test'
  -->
  <button @click="handleClick">clickMe</button>
  <h2
      v-if="count < 6"
      v-change="count">it is a custom directive</h2>
</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('指令已經(jīng)綁定到元素了');
          console.log(el);
          console.log(bindings);
          //準(zhǔn)備將傳遞來的參數(shù)
          // 顯示在調(diào)用該指令的元素的innerHTML
          el.innerHTML = bindings.value;
        },
        update: function (el,bindings) {
          console.log('指令的數(shù)據(jù)有所變化');
          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>

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試,可得到如下運(yùn)行效果:

vue自定義指令的創(chuàng)建和使用方法實(shí)例分析

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <script src="https://cdn.bootcss.com/vue/2.0.1/vue.min.js"></script>
  <title>www.jb51.net vue自定義指令</title>
</head>
<body>
<div id="container">
  <p>{{msg}}</p>
  <h2 v-change-background-color="myBgColor">
    it is a header1
  </h2>
</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>

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試,可得到如下運(yùn)行效果:

vue自定義指令的創(chuàng)建和使用方法實(shí)例分析

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

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI