qx
6 小时以前 ad54cf427ee94e01e8c72a01f738615eb2b6569d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<template>
  <div ref="showPanel" class="text-copy" :class="{ 'box-focus': isShow }" @click="handleCopy">
    <div class="body">
      <span class="pl10">{{ text }}</span>
      <el-link
        v-if="isShow"
        v-clipboard:copy="text"
        v-clipboard:error="
          () => {
            msgError('复制失败')
          }
        "
        v-clipboard:success="
          () => {
            msgSuccess('复制成功')
          }
        "
        class="copy-btn mr10"
        :underline="false"
        @click.stop="() => {}"
      >
        <i class="el-icon-view el-icon-copy-document" />复制
      </el-link>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'TextCopy',
  props: {
    text: {
      type: [String, Number, Object, Array],
      default: ''
    }
  },
  data() {
    return {
      isShow: false
    }
  },
  mounted() {
    document.addEventListener('click', this.bodyCloseCopy)
  },
  beforeDestroy() {
    document.removeEventListener('click', this.bodyCloseCopy)
  },
  methods: {
    bodyCloseCopy(e) {
      if (this.$refs.showPanel) {
        let isSelf = this.$refs.showPanel.contains(e.target)
        if (!isSelf) {
          this.isShow = false
        }
      }
    },
    handleCopy() {
      this.isShow = true
    }
  }
}
</script>
 
<style lang="scss" scoped>
.text-copy {
  position: relative;
  display: flex;
  flex-direction: column;
  line-height: 34px;
  min-height: 34px;
  word-break: break-all;
  background: #f6f8f9;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  cursor: pointer;
  width: 95%;
 
  .copy-btn {
    float: right;
    width: 45px;
  }
}
 
.box-focus {
  border: 1px solid #409eff;
}
</style>