vue 手写组件隐藏手机号和身份证号,并可以复制

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
<template >
  <div>
    <span>{{ value }}</span>
    <i v-if="value != '' " @click="copyValue()" style="margin-left: 10px; color: blue; cursor:pointer" class="el-icon-copy-document"/>
  </div>
</template>
 
<script>
 
export default {
  data() {
    return {
      value: this.changeValue()
    }
  },
  props: {
    replaceValue: {
      type: '',
      default: ""
    },
    replaceType: {
      type: '',
      default: "phone"
    }
  },
  methods:{
    // 复制功能
    copyValue(){
      let content = this.replaceValue
      if (window.clipboardData) {
        window.clipboardData.setData('text', content);
      } else {
        (function (content) {
          document.oncopy = function (e) {
            e.clipboardData.setData('text', content);
            e.preventDefault(); //取消事件的默认动作
            document.oncopy = null;
          }
        })(content);
        document.execCommand('Copy');
        this.$message({
          message: '复制成功',
          type: 'success'
        })
      }
    },
    changeValue(){
        if(this.replaceValue){
          if(this.replaceType == " 手机号字段 "){ //备注: 要和使用组件的地方的字段相对应才能解析
            return this.replaceValue.replace(/^(.{3}).*(.{4})$/,'$1****$2');
          }else if(this.replaceType == " 身份证号码字段 "){//备注: 要和使用组件的地方的字段相对应才能解析return this.replaceValue.replace(/^(.{6})(?:\w+)(.{4})$/,'$1********$2'); } }else{ return ""; } }, } } <br></script>

  

在 vue 项目的 components 里创建一个文件夹和一个 index.vue 文件,然后把上面的代码复制进去就行。

@mousemove="onmouseenters" //事件冒泡处理其他操作
      @mouseout="onmouseleaves"
      v-if="!valueStatus" // 是否隐藏号码

 

 

 

 在 main.js 全局挂载

1
2
3
// 脱敏组件
import TransTo from '@/components/Transitive'
Vue.component('TransTo', TransTo)

  

最后是 使用方法

1
2
3
4
5
6
7
8
9
// 手机号的使用
 
<TransTo :replaceValue="scope.row.phone" :key="Math.random()" />
 
//身份证的使用
 
<TransTo :replaceValue="scope.row.cardNo" replaceType="cardNo" :key ="Math.random()" />
 
//注意: :key ="Math.random()" 这一行是必须写的,因为不写会导致拿不到正确的数据,或者说是数据没有更新过来

  // 备注:如果出现报错: Cannot read properties of undefined (reading 'key') 可以通过格式化解决,即去除小数点解决

代码:

1
:key="Math.random().toFixed(0) * 1"

  

 

posted @   柠檬-不加糖  阅读(924)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示