Vue implements one-click copy text

  <p ref="num">SF1111000000</p>
  <button @click="toCopy">复制</button>

 data() {
    
    
    return {
    
    
      copyTxt: '',
    };
  },
  methods: {
    
    
    toCopy() {
    
    
       this.copyTxt= this.$refs.num.innerText;
       var input = document.createElement("input"); // 直接构建input
       input.value = this.copyTxt; // 设置内容
       console.log(input.value);

       document.body.appendChild(input); // 添加临时实例
       input.select(); // 选择实例内容
       document.execCommand("Copy"); // 执行复制
       document.body.removeChild(input); // 删除临时实例
     },
  }

Guess you like

Origin blog.csdn.net/weixin_48585264/article/details/126700973