vue如何监听键盘事件
(1)@keyup 方法
<template> <input ref="myInput" type="text" value="hello world" autofocus @keyup.enter="handleKey"> </template> <script> export default { methods: { handleKey(e) { console.log(e) } } } </script>
(2)addEventListener
<script> export default { mounted() { document.addEventListener('keyup', this.handleKey) }, beforeDestroy() { document.removeEventListener('keyup', this.handleKey) }, methods: { handleKey(e) { console.log(e) } } } </script>