记录一个复制方法

24 阅读1分钟
  const copyToClipboard = async (url: string) => {
    try {
      if (navigator.clipboard && navigator.clipboard.writeText) {
        await navigator.clipboard.writeText(url)
        message.success('链接复制成功')
      } else {
        // 备用方法
        const textArea = document.createElement('textarea')
        textArea.value = url
        document.body.appendChild(textArea)
        textArea.select()
        document.execCommand('copy')
        document.body.removeChild(textArea)
        message.success('链接复制成功')
      }
    } catch (err) {
      message.error('链接复制失败')
    }
  }

求更多更优秀的复制方案