Skip to content

useClipboard

类别
导出体积
1.46 kB
上次更改
2 months ago
相关

响应式的 Clipboard API。提供对剪贴板命令(剪切、复制和粘贴)的响应能力,以及异步读取和写入系统剪贴板的能力。访问剪贴板内容的权限受到 Permissions API 的限制。未经用户许可,不允许读取或更改剪贴板内容。

通过 Vue School 的这个免费视频课程学习如何使用 useClipboard 将文本响应式保存到剪贴板!

示例

你的浏览器不支持剪贴板 API

使用方法

vue
<script>
import { 
useClipboard
} from '@vueuse/core'
const
source
= ref('Hello')
const {
text
,
copy
,
copied
,
isSupported
} =
useClipboard
({
source
})
</script> <template> <
div
v-if="
isSupported
">
<
button
@
click
="
copy
(
source
)">
<!-- 默认情况下,`copied` 将在 1.5 秒后重置 --> <
span
v-if="!
copied
">复制</
span
>
<
span
v-else>已复制!</
span
>
</
button
>
<
p
>当前已复制的内容: <
code
>{{
text
|| 'none' }}</
code
></
p
>
</
div
>
<
p
v-else>
你的浏览器不支持 Clipboard API </
p
>
</template>

选项(Options)

选项类型默认值说明
sourceMaybeRefOrGetter<string>当调用 copy() 无参数时,默认复制的内容
readbooleanfalse是否在复制/剪切事件发生时读取剪贴板内容
copiedDuringnumber1500copied 状态重置为 false 之前的毫秒数
legacybooleanfalse如果 Clipboard API 不可用,则回退使用 document.execCommand 复制

返回值(Return Values)

属性类型说明
isSupportedComputedRef<boolean>是否支持剪贴板(原生或回退方案)
textRef<string>当前剪贴板内容(当 read: true 时生效)
copiedRef<boolean>复制成功后为 true,自动重置
copy(text?: string) => Promise<void>复制文本到剪贴板

兼容模式(Legacy Mode)

如果 Clipboard API 不可用,设置 legacy: true 来保留复制的能力。它将使用 execCommand 作为回退处理复制。

ts
const { 
copy
,
isSupported
} = useClipboard({
legacy
: true })

组件使用

vue
<template>
  <UseClipboard v-slot="{ 
copy
,
copied
}"
source
="copy me">
<
button
@
click
="
copy
()">
{{
copied
? '已复制' : '复制' }}
</
button
>
</UseClipboard> </template>

类型声明

ts
export interface 
UseClipboardOptions
<
Source
> extends ConfigurableNavigator {
/** * 启用剪贴板读取 * * @default false */
read
?: boolean
/** * 复制的数据源 */
source
?:
Source
/** * 重置 `copied` ref 状态的毫秒数 * * @default 1500 */
copiedDuring
?: number
/** * 如果剪贴板未定义,是否回退到 document.execCommand('copy')。 * * @default false */
legacy
?: boolean
} export interface
UseClipboardReturn
<
Optional
> extends Supportable {
text
:
Readonly
<
ShallowRef
<string>>
copied
:
Readonly
<
ShallowRef
<boolean>>
copy
:
Optional
extends true
? (
text
?: string) =>
Promise
<void>
: (
text
: string) =>
Promise
<void>
} /** * 响应式 Clipboard API. * * @see https://vueuse.org/useClipboard * @param options * * @__NO_SIDE_EFFECTS__ */ export declare function
useClipboard
(
options
?:
UseClipboardOptions
<undefined>,
):
UseClipboardReturn
<false>
export declare function
useClipboard
(
options
:
UseClipboardOptions
<
MaybeRefOrGetter
<string>>,
):
UseClipboardReturn
<true>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
Vida Xie
SerKo
Jelf
Sanxiaozhizi
huajianjiu
broBinChen
NoiseFan
IlyaL
IlyaL
Бєляєв Віталій
Robin
Fernando Fernández
Alex Liu
Indrek Ardel
Mr.Hope
Alexzvn
Cat1007
丶远方
Olusola Olawale
Lumdzeehol
Lumdzeehol
Curt Grimes
wheat
洪布斯
karma
Shinigami
Alex Kozack
Antério Vieira

更新日志

ef0c4 - fix: add readonly attribute to textarea fallback to support Safari 15 (#5179)
67812 - refactor!: use readonly() instead of type assertion Computed (#5081)
8c521 - feat(components)!: refactor components and make them consistent (#4912)
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
a54c4 - fix: unhandled rejection on read permission prompt (#4615)
01acd - feat: should fall back to legacy clipboard when read/write fails (#4512)
dd316 - feat: use passive event handlers everywhere is possible (#4477)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
6860f - fix(useClipboard,useClipboardItems): avoid running "copied" timeout during initialization (#4299)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
a9f02 - fix: fix issue when permission is not defined (#3812)
71b46 - feat: UseClipboard component (#3359)
37e86 - fix: use legacy way when without permission (#3379)