vue在body上面绑定enter事件

时间:2021-12-06 09:00:08
mounted () {
this.bodyListener = (e) => {
if (e.keyCode === 13 && e.target === document.body){
if(!this.cashierDialog){
document.getElementById('cashier-cash-btn').click()
}else{
document.getElementById('cashier-dialog-submit').click()
}
}
if(this.cashierDialog){
if (e.keyCode === 27 && e.target === document.body){
document.getElementById('cashier-dialog-cancel').click()
}
}
}
document.body.addEventListener('keyup', this.bodyListener, false)
},
//在实例销毁之前,从body上面移除这个方法
beforeDestroy() {
document.body.removeEventListener('keyup', this.bodyListener)
},

本文转载自:https://www.cnblogs.com/ilovexiaoming/p/7692067.html