前言:pc端的登錄用回車更簡單方便,但是element的鍵盤事件好像只有input可以加,也只能選中才可觸發,非常不友好
在網上找到如下解決方法:
<el-button @click.native.prevent="handleLogin" @keyup.enter.native="loginEnter('loginData')">登錄</el-button>
加入這段代碼:
created(){
var _self = this;
document.onkeydown = function(e){
if(window.event == undefined){
var key = e.keyCode;
}else{
var key = window.event.keyCode;
}
if(key == 13){
_self.loginEnter('loginData');
}
}
}
就可以直接用了:
methods:{
loginEnter(){
// 加入axios代碼 ...
}
}
但是今天發現這樣好用是好用 但是登錄後再次點擊回車還會觸發此事件 又加入如下代碼:
methods:{
loginEnter(){
// 加入axios代碼
this.$axios({
}).then(function(result){
// 在請求成功後把document.onkeydown置爲undefined
document.onkeydown = undefined;
}).catch(function(err){
})
}
}
——————————————————————————分割線———————————————————————————
開始我看見this.$axios({})不知道幹啥用的,後來才知道和我寫的this.$fetch 和this.$post是一樣的
以下是我的登錄方法
貼一下如果不加以上代碼會產生什麼問題
原文作者:julying
原文鏈接:https://blog.csdn.net/julying/article/details/83587370