vue 复制文本

copy(text) {
     // text: 文本内容
      window.navigator.clipboard
        .writeText(text)
        .then(() => {
          ui.success('复制成功');
        })
        .catch(() => {
          const input = document.createElement('input');
          document.body.appendChild(input);
          input.value = text;
          input.focus();
          input.select();
          const result = document.execCommand('copy');
          input.style.display = 'none';
          if (result === 'unsuccessful') {
            ui.error('复制失败,请手动复制!');
            return;
          }
          ui.success('复制成功');
        });
}

注意:在192.168.xx.xx地址上使用这个方法获取不到clipboard,复制会失败。
需要是localhost、https或者127.0.0.1。

你可能感兴趣的:(vue 复制文本)