本文實例講述了vue自定義鍵盤信息、監(jiān)聽數(shù)據(jù)變化的方法。分享給大家供大家參考,具體如下:
@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>監(jiān)聽數(shù)據(jù)變化:
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('發(fā)生變化了');//淺監(jiān)聽,json里面某個屬性變,是不會監(jiān)聽到的 }); document.onclick=function(){ vm.json.name='aaa'; }; }; </script></head><body> <div id="box"> {{json | json}}//json過濾相當(dāng)于 JSON.string <br> {{b}} </div></body></html>vm.$watch(name,fnCb,{deep:true}); //深度監(jiān)視
<!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('發(fā)生變化了'); },{deep:true}); document.onclick=function(){ vm.json.name='aaa'; }; }; </script></head><body> <div id="box"> {{json | json}} <br> {{b}} </div></body></html>希望本文所述對大家vue.js程序設(shè)計有所幫助。
新聞熱點
疑難解答