index.ts 571 B

12345678910111213141516171819202122232425
  1. import { Directive } from 'vue'
  2. import Clipboard from 'clipboard'
  3. /**
  4. * 复制文本到剪贴板
  5. */
  6. export const copy: Directive = {
  7. mounted(el, binding) {
  8. const fn = binding.value
  9. el.clipboard = new Clipboard(el)
  10. el.clipboard.on('success', () => {
  11. if (fn instanceof Function) {
  12. fn(true)
  13. }
  14. })
  15. el.clipboard.on('error', () => {
  16. if (fn instanceof Function) {
  17. fn(false)
  18. }
  19. })
  20. },
  21. unmounted(el) {
  22. el.clipboard.destroy()
  23. }
  24. }