index.vue 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. console.log('gengxin ')
  31. this.getTextContent()
  32. },
  33. methods: {
  34. getTextContent() {
  35. const slot = this.$slots.default || []
  36. const text = slot[0]?.text || this.text
  37. this.content = text
  38. return text
  39. },
  40. copyText() {
  41. const text = this.content
  42. if (text) {
  43. copy(text)
  44. if (this.hint) {
  45. this.$message.success('复制成功')
  46. }
  47. }
  48. }
  49. }
  50. }
  51. </script>
  52. <style lang="less" scoped>
  53. .copy{
  54. .el-icon-document-copy{
  55. cursor: pointer;
  56. }
  57. }
  58. </style>