vue自定義鍵盤信息、監(jiān)聽數(shù)據(jù)變化的方法示例【基于vm.$watch】
本文實(shí)例講述了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里面某個(gè)屬性變,是不會(huì)監(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>
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
Vue Antd table組件實(shí)現(xiàn)服務(wù)端排序方式
這篇文章主要介紹了Vue Antd table組件實(shí)現(xiàn)服務(wù)端排序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
element el-tooltip動(dòng)態(tài)顯示隱藏(有省略號(hào)顯示,沒有省略號(hào)不顯示)
本文主要介紹了element el-tooltip動(dòng)態(tài)顯示隱藏,主要實(shí)現(xiàn)有省略號(hào)顯示,沒有省略號(hào)不顯示,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
vue中filter的應(yīng)用場(chǎng)景詳解
這篇文章主要為大家介紹了vue中filter的應(yīng)用場(chǎng)景,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
快速解決Vue、element-ui的resetFields()方法重置表單無效的問題
這篇文章主要介紹了快速解決Vue、element-ui的resetFields()方法重置表單無效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
探索Vue中組合式API和選項(xiàng)式API的用法與比較
Vue3為我們開發(fā)提供了兩種組件邏輯實(shí)現(xiàn)方式:選項(xiàng)式API和組合式API,本文將嘗試為大家分析什么是選項(xiàng)式API和組合式API,以及兩種API的優(yōu)缺點(diǎn),希望對(duì)大家有所幫助2023-12-12
vue項(xiàng)目如何從session中獲取對(duì)象,并且使用里面的屬性
這篇文章主要介紹了vue項(xiàng)目如何從session中獲取對(duì)象,并且使用里面的屬性問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

