feat: 点击复制文本方法
parent
6ea379cd00
commit
a5c3252087
@ -0,0 +1,31 @@
|
||||
<template>
|
||||
<el-card>
|
||||
<span class="mr-5">{{ text }}</span>
|
||||
<el-button
|
||||
type="primary"
|
||||
@click="copyText(text)"
|
||||
>点击复制</el-button
|
||||
>
|
||||
</el-card>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref } from 'vue';
|
||||
import { message } from '@/utils/message';
|
||||
|
||||
const text = ref('需要复制的文本');
|
||||
const copyText = (text) => {
|
||||
try {
|
||||
navigator.clipboard.writeText(text);
|
||||
message('复制成功', { type: 'success' });
|
||||
} catch (e) {
|
||||
var textarea = document.createElement('textarea');
|
||||
textarea.value = text;
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(textarea);
|
||||
message('复制成功', { type: 'success' });
|
||||
}
|
||||
};
|
||||
</script>
|
||||
Loading…
Reference in New Issue