本文實例講述了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;
監聽數據變化:
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> {} </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> {} </div></body></html>
希望本文所述對大家vue.js程序設計有所幫助。
新聞熱點
疑難解答