跳到主要内容

useShare

分类
导出大小
360 B
上次更改
上个月

响应式的 Web Share API。浏览器提供可以分享文本或文件内容的功能。

share 方法必须在用户手势(例如按钮点击)之后调用。例如,它不能简单地在页面加载时调用。这是为了防止滥用。

演示

用法

ts
import { 
useShare
} from '@vueuse/core'
const {
share
,
isSupported
} =
useShare
()
function
startShare
() {
share
({
title
: 'Hello',
text
: 'Hello my friend!',
url
:
location
.
href
,
}) }

传入源引用

你可以传入一个 ref,源引用的更改将反映到你的分享选项中。

ts
import { 
ref
} from 'vue'
const
shareOptions
=
ref
<
ShareOptions
>({
text
: 'foo' })
const {
share
,
isSupported
} = useShare(
shareOptions
)
shareOptions
.value.text = 'bar'
share
()
js
import { ref } from 'vue'
const shareOptions = ref({ text: 'foo' })
const { share, isSupported } = useShare(shareOptions)
shareOptions.value.text = 'bar'
share()

类型声明

ts
export interface UseShareOptions {
  
title
?: string
files
?: File[]
text
?: string
url
?: string
} /** * Reactive Web Share API. * * @see https://vueuse.org.cn/useShare * @param shareOptions * @param options * * @__NO_SIDE_EFFECTS__ */ export declare function
useShare
(
shareOptions
?:
MaybeRefOrGetter
<UseShareOptions>,
options
?:
ConfigurableNavigator
,
): {
isSupported
:
ComputedRef
<boolean>
share
: (
overrideOptions
?:
MaybeRefOrGetter
<UseShareOptions>) =>
Promise
<void>
} export type
UseShareReturn
=
ReturnType
<typeof
useShare
>

来源

源代码演示文档

贡献者

Anthony Fu
Antério Vieira
SerKo
Anthony Fu
Jelf
Arthur Darkstone
IlyaL
无尘室内点墨
三咲智子 Kevin Deng
Shinigami
João Eudes Lima

更新日志

61e1b - 修复:提高共享逻辑的准确性 (#5048)
v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数

根据 MIT 许可证发布。