快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-auto`
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/auto/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.
你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-auto` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
-
安装软件包
终端窗口 npm i @capgo/capacitor-auto -
同步原生项目
终端窗口 npx cap sync -
设置根 car 模板
import { Auto } from '@capgo/capacitor-auto';await Auto.setRootTemplate({title: 'Garage',sections: [{header: 'Doors',items: [{id: 'open-main-door',title: 'Open main door',subtitle: 'Tap to send the action to the phone app',payload: { doorId: 'main' },},],},],}); -
处理 car 操作
await Auto.addListener('carAction', async (event) => {if (event.id === 'open-main-door') {await openGarageDoor(event.payload?.doorId);}}); -
追踪连接状态
const status = await Auto.isAvailable();console.log(status.available, status.connected, status.platform);await Auto.addListener('connectionChanged', (event) => {console.log('Car connected:', event.connected, event.platform);});
快速入门
章节标题为“快速入门”import { Auto } from '@capgo/capacitor-auto';
const result = await Auto.isAvailable();console.log(result);示例:setRootTemplate
标题为“示例:setRootTemplate”的部分import { Auto } from '@capgo/capacitor-auto';
await Auto.setRootTemplate({ title: 'Hello from Capacitor', sections: [ { items: [ { id: 'id-123', title: 'Hello from Capacitor', }, ], }, ],});示例:setState
标题为“示例:setState”的部分import { Auto } from '@capgo/capacitor-auto';
await Auto.setState({ key: 'key-123', value: {},});常见坑点
章节标题为“常见陷阱”- 监听器: 在页面卸载时移除句柄。
- 原生同步:运行
npx cap sync在插件升级后。 - 唯一可信来源:信任
src/definitions.ts在 GitHub 仓库中。
空状态
标题为“空状态”的区块设置 emptyText 当模板中没有行时:
await Auto.setRootTemplate({ title: 'Garage', emptyText: 'No actions are available right now.', sections: [],});发送 bridge 消息
标题为“发送 bridge 消息”的章节使用 sendMessage 用于应用程序定义的 native bridge 消息:
await Auto.sendMessage({ type: 'status', payload: { online: true },});平台设置
标题为“平台设置”的章节- 按照 iOS 配置 关于 CarPlay 场景和 entitlement 的说明页面。
- 按照 Android 配置 Android Auto 类别和 manifest 说明页。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划原生插件工作,请将其与 使用 @capgo/capacitor-auto 用于“使用 @capgo/capacitor-auto”中的原生能力, Capgo 插件目录 查看 Capgo 插件目录中的产品工作流, Capgo 提供的 Capacitor 插件 查看由 Capgo 提供的 Capacitor 插件中的实现细节, 添加或更新插件 关于“添加或更新插件”中的实现细节,以及 Ionic Enterprise 插件替代方案 关于“Ionic Enterprise 插件替代方案”中的产品工作流。