跳到主要內容

useShare

分類
導出大小
365 B
最後變更
5 天前

反應式的 Web Share API。瀏覽器提供可以分享文字或檔案內容的功能。

share 方法必須在使用者手勢(例如按鈕點擊)之後呼叫。例如,它不能簡單地在頁面載入時呼叫。這是為了防止濫用。

示範

用法

js
import { useShare } from '@vueuse/core'

const { share, isSupported } = useShare()

function startShare() {
  share({
    title: 'Hello',
    text: 'Hello my friend!',
    url: location.href,
  })
}

傳遞 source ref

您可以傳遞一個 ref 給它,來自 source 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
}
/**
 * Reactive Web Share API.
 *
 * @see https://vueuse.dev.org.tw/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>

原始碼

SourceDemoDocs

貢獻者

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

更新日誌

v12.8.0 on 3/5/2025
7432f - feat(types):棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生的 (#4636)
v12.3.0 on 1/2/2025
59f75 - feat(toValue):棄用來自 @vueuse/sharedtoValue,改用 Vue 原生的
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!:將 MaybeComputedRef 重新命名為 MaybeRefOrGetter
0a72b - feat(toValue):將 resolveUnref 重新命名為 toValue

以 MIT 許可證發布。