import { Directive, onUnmounted, DirectiveBinding } from 'vue' export const keyboard: Directive = { mounted (el, binding: DirectiveBinding) { const handleKeyPress = (event) => { if (event.key === 'Enter') { binding.value() // 执行绑定的方法 } } el.addEventListener('keypress', handleKeyPress) // 在组件卸载时移除事件监听器 onUnmounted(() => { el.removeEventListener('keypress', handleKeyPress) }) } }