本文實例總結了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.49028c.com 自定義指令寫法一</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.49028c.com 自定義指令寫法二</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.49028c.com 自定義指令:拖拽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.49028c.com 自定義元素指令</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.VeVB.COm/code/HtmlJsRun測試上述代碼運行效果。
希望本文所述對大家vue.js程序設計有所幫助。
新聞熱點
疑難解答