在Safari浏览器中直接使用 navigator.clipboard.writeText 是可以直接复制的,没有问题,但是如果在一个promise 后使用 navigator.clipboard.writeText 就会报一个没有权限的错误,具体错误如下
Unhandled Rejection (NotAllowedError): The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
不多说,直接上解决代码
const clipboardItemOut = new ClipboardItem({
"text/plain": shareCandidate(spaceAuth.spaceCode, candidate_id).then(
data => {
let text = `${window.location.origin}/space/${data.data.code}`
if (data.data.passcode) {
text += `\npasscode: ${data.data.passcode}`
}
return new Blob([text], { type: 'text/plain' })
}
)
})
await navigator.clipboard.write([clipboardItemOut])
message.success("Link copied to clipboard!")