Appearance
copyText 复制文本
function copyText(text: string): Promise<string>
复制文本到剪贴板。返回 Promise,复制成功时 resolve 被复制的文本。
参数
| 参数名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| text | 需要复制的文本 | string | 必填 |
返回值
| 返回值 | 说明 | 类型 |
|---|---|---|
| promise | 成功时 resolve 原文本,失败时 reject 错误 | Promise<string> |
示例
基础用法
typescript
import { copyText } from '@mingto/tools'
await copyText('Hello mt-utils')按钮点击复制
typescript
import { copyText } from '@mingto/tools'
async function handleCopy() {
try {
const text = await copyText('邀请码:MT2026')
console.log(`已复制:${text}`)
}
catch (error) {
console.error('复制失败', error)
}
}注意事项
- 该函数依赖浏览器
document与execCommand能力。 - 复制操作通常需要由用户交互触发,否则可能被浏览器策略限制。