复制文本内容

新建utils.js

import { Message } from 'element-ui';
export function copySQl(str) {
  var cinput = document.createElement('textarea');
  cinput.value = str;
  cinput.setAttribute('readOnly', 'true');
  //注意大小写,不写的话手机端会自动弹出输入框
  document.body.appendChild(cinput);
  cinput.select(); // 选择对象
  document.execCommand('Copy'); // 执行浏览器复制命令
  cinput.className = 'oInput';
  cinput.style.display = 'none';
  cinput.style.opacity = '0';
  Message({
    message: '复制成功',
    type: 'success',
    duration: 1 * 1000,
    showClose: true
  });
}

  组件中使用

import { copySQl } from '@/modules/datacatalog/utils/utils';

  

copySQl(str) {
      copySQl(str);
    },

  

<p class="script-title">
                <span> 脚本{{ i + 1 }}({{ item.type }}): </span>
                <i
                  class="el-icon-document"
                  v-if="item.content"
                  @click="copySQl(item.content)"
                ></i>
              </p>

  

posted @ 2023-07-10 16:06  你丫才美工  阅读(10)  评论(0编辑  收藏  举报