开始使用
复制一个包含安装步骤和本插件的完整 Markdown 指南的配置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-share-target`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/share-target/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
安装bun add @capgo/capacitor-share-targetbunx cap sync导入
导入import { CapacitorShareTarget } from '@capgo/capacitor-share-target';API概述
API概述addListener
添加监听器监听分享接收事件。
注册一个监听器,当内容从另一个应用程序分享到应用程序时会被调用。回调函数接收事件数据,包含标题、文本和文件。
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
const listener = await CapacitorShareTarget.addListener('shareReceived', (event) => { console.log('Title:', event.title); console.log('Texts:', event.texts); event.files?.forEach(file => { console.log(`File: ${file.name} (${file.mimeType})`); });});
// To remove the listener:await listener.remove();removeAllListeners
移除所有监听器移除此插件的所有监听器。
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
await CapacitorShareTarget.removeAllListeners();getPluginVersion
获取插件版本获取本地Capacitor插件版本。
返回本地插件实现的当前版本。
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
const { version} = await CapacitorShareTarget.getPluginVersion();console.log('Plugin version:', version);类型参考
类型参考ShareReceivedEvent
分享接收事件当内容被分享到应用程序时接收的事件数据。
export interface ShareReceivedEvent { /** * The title of the shared content. * * @since 0.1.0 */ title: string;
/** * Array of text content shared to the application. * * @since 0.1.0 */ texts: string[];
/** * Array of files shared to the application. * * @since 0.2.0 */ files: SharedFile[];}SharedFile
共享文件表示被分享到应用程序的文件。
export interface SharedFile { /** * The URI of the shared file. On Android/iOS this will be a file path or data URL. * On web this will be a cached URL accessible via fetch. * * @since 0.1.0 */ uri: string;
/** * The name of the shared file, with or without extension. * * @since 0.1.0 */ name: string;
/** * The MIME type of the shared file. * * @since 0.1.0 */ mimeType: string;}真实来源
真实来源本页面由插件生成。 src/definitions.ts当公共API在上游发生变化时,请重新同步。