index.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <span class="copyWrap">
  3. <el-tooltip effect="dark" placement="top" :content="content" v-if="content">
  4. <span class="copy" :style="{ width: width }">
  5. <slot />
  6. </span>
  7. </el-tooltip>
  8. <i
  9. v-if="!!content"
  10. @click.stop="copyText"
  11. title="复制"
  12. class="el-icon-document-copy"
  13. ></i>
  14. </span>
  15. </template>
  16. <script>
  17. import copy from "copy-to-clipboard";
  18. export default {
  19. name: "copy-text",
  20. props: {
  21. text: {
  22. type: String,
  23. default: "",
  24. },
  25. hint: {
  26. type: Boolean,
  27. default: true,
  28. },
  29. width: {
  30. type: String,
  31. default: "auto",
  32. },
  33. },
  34. data() {
  35. return {
  36. content: "",
  37. };
  38. },
  39. mounted() {
  40. this.getTextContent();
  41. },
  42. updated() {
  43. this.getTextContent();
  44. },
  45. methods: {
  46. getTextContent() {
  47. const slot = this.$slots.default || [];
  48. const text = slot[0]?.text || this.text;
  49. this.content = text;
  50. return text;
  51. },
  52. copyText(e) {
  53. const text = this.content.replace(/(^\s*)|(\s*$)/g, "");
  54. if (text) {
  55. copy(text);
  56. if (this.hint) {
  57. this.$message.success("复制成功");
  58. }
  59. }
  60. },
  61. },
  62. };
  63. </script>
  64. <style lang="less" scoped>
  65. .copyWrap {
  66. display: flex;
  67. align-items: center;
  68. width: 100%;
  69. margin: 0 auto;
  70. justify-content: center;
  71. .copy {
  72. overflow: hidden;
  73. text-overflow: ellipsis;
  74. white-space: nowrap;
  75. display: inline-block;
  76. }
  77. .el-icon-document-copy {
  78. cursor: pointer;
  79. margin-left: 2px;
  80. }
  81. }
  82. </style>