?? 利用execCommand復(fù)制文本
<template>
<button @click="copyText">復(fù)制</button>
<section id="copy-text">
...
</section>
</template>
// 復(fù)制監(jiān)測詳情內(nèi)容
const copyText = () => {
// 獲取需要復(fù)制的元素以及元素內(nèi)的文本內(nèi)容
const container = document.getElementById('copy-text');
const text = container.innerText;
// 添加一個input元素放置需要的文本內(nèi)容
const copyContent= document.createElement('input');
copyContent.value = text;
document.body.appendChild(copyContent);
// 選中并復(fù)制文本到剪切板
copyContent.select();
document.execCommand('copy');
// 移除input元素
document.body.removeChild(copyContent);
console.log('復(fù)制成功');
};
需要注意的是蚯撩,復(fù)制的文本不會換行础倍。
后來又要求復(fù)制的文本以固定格式輸出,沒有辦法只能硬寫,取到各種需要的數(shù)據(jù)拼接成字符串沟启。(我是笨比,不知道有啥更好的方法??)