clipboard.js 687 B

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