這篇文章主要介紹了vue自定義鍵盤信息、監聽數據變化的方法,結合實例形式分析了vue.js基於vm.$watch進行事件監聽相關操作技巧,需要的朋友可以參考下
本文實例講述了vue自定義鍵盤信息、監聽數據變化的方法。分享給大家供大家參考,具體如下:
@keydown.up
@keydown.enter
@keydown.a/b/c....
自定義鍵盤信息:
Vue.directive('on').keyCodes.ctrl=17; Vue.directive('on').keyCodes.myenter=13;
@keydown.a/b/c....
<input type="text" @keydown.c="show">
自定義鍵盤信息:
Vue.directive('on').keyCodes.ctrl=17; Vue.directive('on').keyCodes.myenter=13;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> </style> <script src="vue.js"></script> <script> Vue.directive('on').keyCodes.ctrl=17; // Vue.directive('on').keyCodes.myenter=13; window.onload=function(){ var vm=new Vue({ el:'#box', data:{ a:'blue' }, methods:{ show:function(){ alert(1); } } }); }; </script> </head> <body> <div id="box"> <input type="text" @keydown.myenter="show | debounce 2000"> </div> </body> </html>
監聽數據變化:
vm.el/el/mount/$options/....
vm.$watch(name,fnCb); //淺度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="vue.js"></script> <script> window.onload=function(){ var vm=new Vue({ el:'#box', data:{ json:{name:'strive',age:16}, b:2 } }); vm.$watch('json',function(){ alert('發生變化了');//淺監聽,json裏面某個屬性變,是不會監聽到的 }); document.onclick=function(){ vm.json.name='aaa'; }; }; </script> </head> <body> <div id="box"> {{json | json}}//json過濾相當於 JSON.string <br> {{b}} </div> </body> </html>
vm.$watch(name,fnCb,{deep:true}); //深度監視
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="vue.js"></script> <script> window.onload=function(){ var vm=new Vue({ el:'#box', data:{ json:{name:'strive',age:16}, b:2 } }); vm.$watch('json',function(){ alert('發生變化了'); },{deep:true}); document.onclick=function(){ vm.json.name='aaa'; }; }; </script> </head> <body> <div id="box"> {{json | json}} <br> {{b}} </div> </body> </html>
希望本文所述對大家vue.js程序設計有所幫助。