vue自定義指令的創(chuà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.fzitv.net vue自定義指令</title>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<!-- 準(zhǔn)備實現(xiàn)需求:
在h1標(biāo)簽上面,加上一個按鈕,當(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);
//準(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測試,可得到如下運(yùn)行效果:

<!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.fzitv.net 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代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測試,可得到如下運(yùn)行效果:

<h4 v-change-background-color="'red'">背景色</h4>這樣也是可以的,但是寫死了,不好
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章
Vue中Element的table多選表格如何實現(xiàn)單選
這篇文章主要介紹了Vue中Element的table多選表格如何實現(xiàn)單選,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue3+TypeScript實現(xiàn)Docx/Excel預(yù)覽組件
這篇文章主要為大家詳細(xì)介紹了如何使用Vue3+TypeScript實現(xiàn)Docx/Excel預(yù)覽組件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-04-04
Vue2利用Axios發(fā)起請求的詳細(xì)過程記錄
有很多時候你在構(gòu)建應(yīng)用時需要訪問一個API并展示其數(shù)據(jù),做這件事的方法有好幾種,而使用基于promise的HTTP客戶端axios則是其中非常流行的一種,這篇文章主要給大家介紹了關(guān)于Vue2利用Axios發(fā)起請求的詳細(xì)過程,需要的朋友可以參考下2021-12-12
基于vue2.0+vuex+localStorage開發(fā)的本地記事本示例
這篇文章主要介紹了基于vue2.0+vuex+localStorage開發(fā)的本地記事本示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02

