ShareSheet 分享面板
按平台能力过滤分享选项;复制、系统分享和适配器成功后才发 shared。
引入
uni-app 配置 easycom 后使用 <hui-share-sheet>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-space direction="vertical" fill>
<hui-button custom-class="hui-space__item" @click="visible = true">分享 / Share</hui-button>
<hui-share-sheet
v-model:visible="visible"
custom-class="hui-space__item"
:options="options"
:payload="payload"
@select="onSelect"
@shared="(name) => (lastEvent = 'shared:' + name)"
@close="(reason) => (lastEvent = 'close:' + reason)"
@cancel="lastEvent = 'cancel'"
@open="lastEvent = 'open'"
@opened="lastEvent = 'opened'"
@closed="lastEvent = 'closed'"
/>
<text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ShareSheetOption } from '@nscode/handy-uni'
const lastEvent = ref('尚无事件 / No event')
const visible = ref(false)
const onSelect = (option: ShareSheetOption, context: { index: number; row: number }) => {
lastEvent.value = JSON.stringify({ option, context })
}
const options: ('wechat' | 'timeline' | 'link' | 'system')[] = [
'wechat',
'timeline',
'link',
'system'
]
const payload = {
title: '文章 / Article',
desc: '内容摘要 / Summary',
url: 'https://example.com/article',
path: '/pages/article/index?id=1'
}
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
visible | boolean | undefined | v-model:visible |
defaultVisible | boolean | false | v-model:visible |
options | (ShareSheetOption | ShareSheetPreset)[] | (ShareSheetOption | ShareSheetPreset)[][] | [] | 二维数组时分多行,每行可以横向滑动 |
payload | ShareSheetPayload | — | 要分享的内容;预设选项用它 |
title | string | — | 不传用 t('shareSheet.title');传 '' 不显示 |
description | string | — | 标题下的说明 |
cancelText | string | — | 不传用 t('common.cancel');传 '' 不显示(照 相关组件) |
closeOnSelect | boolean | true | 选了以后自动关(照 相关组件 的 closeOnClickAction) |
closeOnClickOverlay | boolean | true | 点击遮罩时请求关闭。 |
beforeClose | (reason: ShareSheetCloseReason) => boolean | void | Promise<boolean | void> | — | 关闭前异步确认,返回 false 或拒绝时保留面板;参数为本次关闭原因。 |
safeAreaInsetBottom | boolean | true | safeAreaInsetBottom 属性。 |
zIndex | number | — | zIndex 属性。 |
rootPortal | boolean | false | 微信小程序使用根门户提升弹层。 |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
update:visible | (visible: boolean) |
select | (option: ShareSheetOption, context: { index:number;row:number }) |
shared | (name: string) |
cancel | () |
close | (reason: ShareSheetCloseReason) |
open | () |
opened | () |
closed | () |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
preview | — |
option | { option, index, row } |
guide | { preset, close } |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
showGuide | (name: string): void | 显示指定分享引导 |
类型
ShareSheetPreset、ShareSheetOption、ShareSheetPayload、ShareSheetCloseReason、ShareSheetProps、ShareSheetEmits、ShareSheetExpose.
平台说明
微信好友用原生 button open-type=share,页面配置 onShareAppMessage;朋友圈显示引导,页面配置 onShareTimeline,二者不伪报 shared。App 的微信/朋友圈依赖 share 适配器;系统分享取消不发 shared。普通浏览器隐藏不支持预设。微信复制使用原生提示,其余端复制成功用 Toast。先关闭面板再显示引导。
设备冒烟清单.