<input v-on:keyup.enter="submit">//局部
//全局
methods: { handleKeyDown(e) { let key = null; if (window.event === undefined) { key = e.keyCode; } else { key = window.event.keyCode; } if (key === 13) { //触发的事件 this.onSubmit(); } }, }, created() { window.addEventListener("keydown", this.handleKeyDown, true); //开启监听键盘按下事件 },
全局添加时,离开本页面时要取消监听
destroyed() { window.removeEventListener("keydown", this.handleKeyDown, true); },