clipboard.js 745B

12345678910111213141516171819202122232425262728293031323334353637
  1. import Vue from 'vue'
  2. import Clipboard from 'clipboard'
  3. function clipboardSuccess() {
  4. Vue.prototype.$message({
  5. message: 'Copy successfully',
  6. type: 'success',
  7. duration: 1500
  8. })
  9. }
  10. function clipboardError() {
  11. Vue.prototype.$message({
  12. message: 'Copy failed',
  13. type: 'error'
  14. })
  15. }
  16. export default function handleClipboard(text, event) {
  17. const clipboard = new Clipboard(event.target, {
  18. text: () => text
  19. })
  20. clipboard.on('success', () => {
  21. clipboardSuccess()
  22. clipboard.off('error')
  23. clipboard.off('success')
  24. clipboard.destroy()
  25. })
  26. clipboard.on('error', () => {
  27. clipboardError()
  28. clipboard.off('error')
  29. clipboard.off('success')
  30. clipboard.destroy()
  31. })
  32. clipboard.onClick(event)
  33. }