Skip to content

useClipboardItems

类别
导出大小
1.03 kB
最近修改
2 days ago
相关

响应式 剪贴板 API。提供响应剪贴板命令(剪切、复制和粘贴)的能力,以及异步读取和写入系统剪贴板的功能。访问剪贴板内容需要通过 权限 API 授权。未经用户许可,不允许读取或修改剪贴板内容。

🌐 Reactive Clipboard API. Provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard. Access to the contents of the clipboard is gated behind the Permissions API. Without user permission, reading or altering the clipboard contents is not permitted.

示例

Your browser does not support Clipboard API

useClipboard 的区别

🌐 Difference from useClipboard

useClipboard 是一个“纯文本”功能,而 useClipboardItems 是一个基于 ClipboardItem 的功能。你可以使用 useClipboardItems 复制任何 ClipboardItem 支持的内容。

用法

🌐 Usage

vue
<script setup lang="ts">
import { 
useClipboardItems
} from '@vueuse/core'
const
mime
= 'text/plain'
const
source
=
ref
([
new
ClipboardItem
({
[
mime
]: new
Blob
(['plain text'], {
type
:
mime
}),
}) ]) const {
content
,
copy
,
copied
,
isSupported
} =
useClipboardItems
({
source
})
</script> <template> <
div
v-if="
isSupported
">
<
button
@
click
="
copy
(
source
)">
<!-- by default, `copied` will be reset in 1.5s --> <
span
v-if="!
copied
">Copy</
span
>
<
span
v-else>Copied!</
span
>
</
button
>
<
p
>
Current copied: <
code
>{{
content
|| 'none' }}</
code
>
</
p
>
</
div
>
<
p
v-else>
Your browser does not support Clipboard API </
p
>
</template>