Skip to content

useShare

Category
Export Size
546 B
Last Changed
15 minutes ago

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

必须在用户操作(如按钮点击)之后调用 share 方法。不能在页面加载时简单地调用它。这是为了防止滥用而设定的。

Demo

用法

js
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()

类型声明

typescript
export interface UseShareOptions {
  title?: string
  files?: File[]
  text?: string
  url?: string
}
/**
 * 响应式 Web Share API.
 * 浏览器提供了可以分享文本或文件内容的功能。
 *
 * @see https://vueuse.org/useShare
 * @param shareOptions
 * @param options
 */
export declare function useShare(
  shareOptions?: MaybeRefOrGetter<UseShareOptions>,
  options?: ConfigurableNavigator,
): {
  isSupported: ComputedRef<boolean>
  share: (overrideOptions?: MaybeRefOrGetter<UseShareOptions>) => Promise<void>
}
export type UseShareReturn = ReturnType<typeof useShare>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Antério Vieira
Jelf
Anthony Fu
无尘室内点墨
三咲智子 Kevin Deng
Shinigami
João Eudes Lima

变更日志

v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue

Released under the MIT License.