vue自定義鍵盤信息、監(jiān)聽數(shù)據(jù)變化的方法示例【基于vm.$watch】
本文實例講述了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> {} </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> {} </div> </body> </html>
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章
vue+elementUI實現(xiàn)多文件上傳與預(yù)覽功能實戰(zhàn)記錄(word/PDF/圖片/docx/doc/xlxs/txt)
這篇文章主要給大家介紹了關(guān)于利用vue+elementUI實現(xiàn)多文件上傳與預(yù)覽功能的相關(guān)資料,包括word/PDF/圖片/docx/doc/xlxs/txt等格式文件上傳,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Vue?運(yùn)行高德地圖官方樣例,設(shè)置class無效的解決
這篇文章主要介紹了Vue?運(yùn)行高德地圖官方樣例,設(shè)置class無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10