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="https://www.jb51.net/article/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> <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="https://www.jb51.net/article/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> {{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="https://www.jb51.net/article/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> {{json | json}} <br> {{b}} </div> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/262b82048826aadf31eb3da5e336cfb0.html