国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-19 12:24:39
字體:
供稿:網(wǎng)友

本文實例講述了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...
但是這些指令都是比較偏向于工具化,有些時候在實現(xiàn)具體的業(yè)務(wù)邏輯的時候,發(fā)現(xiàn)不夠用,如何來自定義指令.

1、自定義指令

① 創(chuàng)建

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

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

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

② 使用自定義指令

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

使用:

v-hello

注意事項:

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

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

bind方法以及update方法 都是有參數(shù)的,
一個是el,對應(yīng)的是調(diào)用指令的元素
一個bindings,是一個對象: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>m.survivalescaperooms.com vue自定義指令</title></head><body><div id="container">  <p>{{msg}}</p>  <!-- 準備實現(xiàn)需求:  在h1標簽上面,加上一個按鈕,當(dāng)點擊按鈕時候,對count實現(xiàn)一次  自增操作,當(dāng)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('指令已經(jīng)綁定到元素了');          console.log(el);          console.log(bindings);          //準備將傳遞來的參數(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代碼運行工具http://tools.VeVB.COm/code/HtmlJsRun測試,可得到如下運行效果:

<!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>m.survivalescaperooms.com vue自定義指令</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>

使用在線HTML/CSS/JavaScript代碼運行工具http://tools.VeVB.COm/code/HtmlJsRun測試,可得到如下運行效果:

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新疆| 淮阳县| 莫力| 汉寿县| 库车县| 虞城县| 温宿县| 小金县| 砚山县| 潍坊市| 伽师县| 砀山县| 鄂托克旗| 包头市| 九江县| 鹿泉市| 绥宁县| 休宁县| 会泽县| 奉化市| 咸宁市| 岳西县| 同仁县| 东莞市| 扶风县| 伊宁市| 兴仁县| 雅江县| 白城市| 乌拉特后旗| 蚌埠市| 勃利县| 天津市| 温宿县| 根河市| 保德县| 枝江市| 八宿县| 托克逊县| 同江市| 新民市|