index.vue 1017 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <span class="copy" v-if="content">
  3. <slot/>{{text}}
  4. <i v-if="!!content" @click="copyText" title="复制" class="el-icon-document-copy"></i>
  5. </span>
  6. </template>
  7. <script>
  8. import copy from 'copy-to-clipboard'
  9. export default {
  10. name: 'copy-text',
  11. props: {
  12. text: {
  13. type: String,
  14. default: ''
  15. },
  16. hint: {
  17. type: Boolean,
  18. default: true
  19. }
  20. },
  21. data() {
  22. return {
  23. content: ''
  24. }
  25. },
  26. mounted() {
  27. this.getTextContent()
  28. },
  29. updated() {
  30. this.getTextContent()
  31. },
  32. methods: {
  33. getTextContent() {
  34. const slot = this.$slots.default || []
  35. const text = slot[0]?.text || this.text
  36. this.content = text
  37. return text
  38. },
  39. copyText() {
  40. const text = this.content
  41. if (text) {
  42. copy(text)
  43. if (this.hint) {
  44. this.$message.success('复制成功')
  45. }
  46. }
  47. }
  48. }
  49. }
  50. </script>
  51. <style lang="less" scoped>
  52. .copy{
  53. .el-icon-document-copy{
  54. cursor: pointer;
  55. }
  56. }
  57. </style>