index.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { message } from 'ant-design-vue'
  2. import { Directive, onUnmounted, DirectiveBinding } from 'vue'
  3. export const keyboard: Directive = {
  4. mounted (el: HTMLDivElement, binding: DirectiveBinding) {
  5. const key = binding.arg
  6. console.log('key:', el, binding)
  7. const handleKeyPress = (event) => {
  8. console.log(event)
  9. if (event.key === key) {
  10. binding.value() // 执行绑定的方法
  11. }
  12. }
  13. el.addEventListener('keydown', () => {
  14. console.log('keydown')
  15. })
  16. el.addEventListener('keyup', () => {
  17. console.log('keyup')
  18. })
  19. el.addEventListener('keypress', handleKeyPress)
  20. // 在组件卸载时移除事件监听器
  21. onUnmounted(() => {
  22. el.removeEventListener('keypress', handleKeyPress)
  23. })
  24. }
  25. }
  26. export const copy: Directive = {
  27. mounted (el: HTMLElement, binding: DirectiveBinding) {
  28. el.addEventListener('click', () => {
  29. const inputValue = document.createElement('input')
  30. document.body.appendChild(inputValue)
  31. inputValue.value = el.innerHTML
  32. inputValue.select()
  33. document.execCommand('copy')
  34. message.success('复制成功')
  35. document.body.removeChild(inputValue)
  36. })
  37. onUnmounted(() => {
  38. el.removeEventListener('click', () => {})
  39. })
  40. }
  41. }