vue自定義指令用法經典實例小結
更新時間:2019年03月16日 11:08:42 作者:白楊-M
這篇文章主要介紹了vue自定義指令用法,結合實例形式總結分析了vue自定義指令常見寫法與相關操作注意事項,需要的朋友可以參考下
本文實例總結了vue自定義指令用法。分享給大家供大家參考,具體如下:
自定義指令:
一、屬性:
Vue.directive(指令名稱,function(參數){ this.el -> 原生DOM元素 });
<div v-red="參數"></div>
指令名稱: v-red -> red
* 注意: 必須以 v-開頭
拖拽:
二、自定義元素指令:(用處不大)
Vue.elementDirective('zns-red',{ bind:function(){ this.el.style.background='red'; } });
自定義指令寫法一:
<div id="box"> <span v-red> asdfasd </span> </div>
Vue.directive('red',function(){ this.el.style.background='red'; }); window.onload=function(){ var vm=new Vue({ el:'#box', data:{ msg:'welcome' } }); };
測試示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.dbjr.com.cn 自定義指令寫法一</title> <script src="https://cdn.bootcss.com/vue/1.0.4/vue.min.js"></script> <script> Vue.directive('red',function(){ this.el.style.background='red'; }); window.onload=function(){ var vm=new Vue({ el:'#box', data:{ msg:'welcome' } }); }; </script> </head> <body> <div id="box"> <span v-red> asdfasd </span> </div> </body> </html>
自定義指令寫法二:推薦寫法
<div id="box"> <span v-red="a"> asdfasd </span> </div>
//這里的color可以傳參 Vue.directive('red',function(color){ this.el.style.background=color; }); window.onload=function(){ var vm=new Vue({ el:'#box', data:{ a:'blue' } }); };
測試示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.dbjr.com.cn 自定義指令寫法二</title> <script src="https://cdn.bootcss.com/vue/1.0.4/vue.min.js"></script> <script> //這里的color可以傳參 Vue.directive('red',function(color){ this.el.style.background=color; }); window.onload=function(){ var vm=new Vue({ el:'#box', data:{ a:'blue' } }); }; </script> </head> <body> <div id="box"> <span v-red="a"> asdfasd </span> </div> </body> </html>
自定義指令寫法三:
<div id="box"> <span v-red> asdfasd </span> </div>
Vue.directive('red',{ bind:function(){ this.el.style.background='red'; } }); window.onload=function(){ var vm=new Vue({ el:'#box' }); };
自定義指令:拖拽drag
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.dbjr.com.cn 自定義指令:拖拽drag</title> <script src="https://cdn.bootcss.com/vue/1.0.4/vue.min.js"></script> <script> Vue.directive('drag',function(){ var oDiv=this.el; oDiv.onmousedown=function(ev){ var disX=ev.clientX-oDiv.offsetLeft; var disY=ev.clientY-oDiv.offsetTop; document.onmousemove=function(ev){ var l=ev.clientX-disX; var t=ev.clientY-disY; oDiv.style.left=l+'px'; oDiv.style.top=t+'px'; }; document.onmouseup=function(){ document.onmousemove=null; document.onmouseup=null; }; }; }); window.onload=function(){ var vm=new Vue({ el:'#box', data:{ msg:'welcome' } }); }; </script> </head> <body> <div id="box"> <div v-drag :style="{width:'100px', height:'100px', background:'blue', position:'absolute', right:0, top:0}"></div> <div v-drag :style="{width:'100px', height:'100px', background:'red', position:'absolute', left:0, top:0}"></div> </div> </body> </html>
自定義元素指令
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.dbjr.com.cn 自定義元素指令</title> <style> zns-red{ width:100px; background: gray; height:100px; display: block; } </style> <script src="https://cdn.bootcss.com/vue/1.0.4/vue.min.js"></script> <script> Vue.elementDirective('zns-red',{ bind:function(){ this.el.style.background='red'; } }); window.onload=function(){ var vm=new Vue({ el:'#box', data:{ a:'blue' } }); }; </script> </head> <body> <div id="box"> <zns-red></zns-red> </div> </body> </html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
希望本文所述對大家vue.js程序設計有所幫助。
相關文章
vue如何修改el-form-item中的label樣式修改問題
這篇文章主要介紹了vue如何修改el-form-item中的label樣式修改問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10VUE3中h()函數和createVNode()函數的使用解讀
這篇文章主要介紹了VUE3中h()函數和createVNode()函數的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue?Router修改query參數url參數沒有變化問題及解決
這篇文章主要介紹了Vue?Router修改query參數url參數沒有變化問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09