vue自定義指令的創(chuàng)建和使用方法實(shí)例分析
本文實(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í)現(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>www.dbjr.com.cn vue自定義指令</title>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<!-- 準(zhǔn)備實(shí)現(xiàn)需求:
在h1標(biāo)簽上面,加上一個按鈕,當(dāng)點(diǎn)擊按鈕時候,對count實(shí)現(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>www.dbjr.com.cn 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實(shí)現(xiàn)導(dǎo)出excel表格功能
這篇文章主要介紹了Vue實(shí)現(xiàn)導(dǎo)出excel表格的功能,在文章末尾給大家提到了vue中excel表格的導(dǎo)入和導(dǎo)出代碼,需要的朋友可以參考下2018-03-03
Vue3數(shù)字滾動插件vue-countup-v3的使用
vue-countup-v3 插件是一個基于 Vue3 的數(shù)字動畫插件,用于在網(wǎng)站或應(yīng)用程序中創(chuàng)建帶有數(shù)字動畫效果的計數(shù)器,本文主要介紹了Vue3數(shù)字滾動插件vue-countup-v3的使用,感興趣的可以了解一下2023-10-10
vue中實(shí)現(xiàn)子組件接收父組件方法并獲取返回值
這篇文章主要介紹了vue中實(shí)現(xiàn)子組件接收父組件方法并獲取返回值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue?3?使用moment設(shè)置顯示時間格式的問題及解決方法
在Vue?3中,因?yàn)檫^濾器(filter)已經(jīng)被廢棄,取而代之的是全局方法(global?method),本文給大家介紹Vue?3?使用moment設(shè)置顯示時間格式的問題及解決方法,感興趣的朋友一起看看吧2023-12-12
antd配置config-overrides.js文件的操作
這篇文章主要介紹了antd配置config-overrides.js文件的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue?this.$router.go(-1);返回時如何帶參數(shù)
這篇文章主要介紹了vue?this.$router.go(-1);返回時如何帶參數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

