开始
复制一个包含安装步骤和本插件的完整 Markdown 指南的配置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-native-navigation`
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/native-navigation/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.
-
安装包
终端窗口 npm i @capgo/capacitor-native-navigation -
同步本地项目
终端窗口 npx cap sync -
配置本地 Chrome
import { NativeNavigation } from '@capgo/capacitor-native-navigation';await NativeNavigation.configure({contentInsetMode: 'css',animationDuration: 360,colors: {tint: '#0f172a',inactiveTint: '#64748b',},}); -
渲染原生导航栏
await NativeNavigation.setNavbar({title: 'Home',subtitle: 'Native chrome',transparent: true,backButton: { visible: false },rightItems: [{id: 'compose',title: 'Compose',icon: {svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/></svg>',},},],}); -
渲染原生标签栏
await NativeNavigation.setTabbar({selectedId: 'home',labelVisibilityMode: 'selected',icons: true,colors: {dynamic: true,tint: '#0f172a',inactiveTint: '#64748b',},tabs: [{id: 'home',title: 'Home',icon: {svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 10.5 12 3l9 7.5"/><path d="M5 10v10h14V10"/></svg>',},},{id: 'settings',title: 'Settings',badge: '2',icon: {svg: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M2 12h3M19 12h3"/></svg>',},},],}); -
处理原生意图事件
await NativeNavigation.addListener('navbarBack', () => {router.back();});await NativeNavigation.addListener('navbarItemTap', ({ id }) => {if (id === 'compose') router.push('/compose');});await NativeNavigation.addListener('tabSelect', ({ id }) => {router.push(`/${id}`);});
过渡流程
标题:过渡流程原生过渡是对正常 JavaScript 路由更改的交易:
const transition = await NativeNavigation.beginTransition({ direction: 'forward',});
router.push('/detail');await router.ready?.();
await NativeNavigation.setNavbar({ title: 'Detail', backButton: { visible: true, title: 'Back' },});
await NativeNavigation.finishTransition({ id: transition.id, direction: 'forward',});缩放过渡
标题:“缩放过渡”使用缩放辅助器进行卡片到详细信息或媒体预览流程。将点击元素传递给路由器之前,内容发生变化,然后在详细页面准备好后完成。
import { beginZoomTransition, finishZoomTransition } from '@capgo/capacitor-native-navigation';
const card = document.querySelector('[data-message-card]');if (card) { const transition = await beginZoomTransition(card, { cornerRadius: 18 });
router.push('/message/42'); await router.ready?.();
await NativeNavigation.setNavbar({ title: 'Message', backButton: { visible: true, title: 'Inbox' }, });
await finishZoomTransition(undefined, { id: transition.id, cornerRadius: 18, });}使用@capgo/capacitor-transitions
标题:“使用@capgo/capacitor-transitions”使用原生导航器进行原生导航栏、标签栏、安全区域内边距和原生意图事件。使用 @capgo/capacitor-transitions 原生导航器
npm install @capgo/capacitor-native-navigation @capgo/capacitor-transitionsnpx cap sync复制到剪贴板
import { NativeNavigation } from '@capgo/capacitor-native-navigation';import '@capgo/capacitor-transitions';import { initTransitions, setupRouterOutlet, setDirection } from '@capgo/capacitor-transitions/react';
initTransitions({ platform: 'auto' });
const outlet = document.querySelector('cap-router-outlet');if (outlet) { setupRouterOutlet(outlet, { platform: 'auto', swipeGesture: 'auto' });}
await NativeNavigation.configure({ contentInsetMode: 'css',});Section titled “Zoom transition” cap-router-outlet 专注于页面,而不是重复的网页栏:
<cap-router-outlet platform="auto" swipe-gesture="auto"> <cap-page> <cap-content slot="content" fullscreen> <main class="page">Inbox content</main> </cap-content> </cap-page></cap-router-outlet>从同一个路由动作驱动两个包:
async function openMessage(id: string) { setDirection('forward'); await router.push(`/messages/${id}`); await NativeNavigation.setNavbar({ title: 'Message', backButton: { visible: true, title: 'Inbox' }, });}
await NativeNavigation.addListener('navbarBack', () => { setDirection('back'); router.back();});
await NativeNavigation.addListener('tabSelect', ({ id }) => { setDirection('root'); router.push(`/${id}`);});在每次路由变化时选择一个动画层。让 @capgo/capacitor-transitions 普通页面推送使用正常动画,而仅在共享元素或缩放路由时使用Native Navigation的缩放助手。
CSS insets
标题:CSS insets使用 contentInsetMode: 'css',插件将原生栏的尺寸写入 document.documentElement.
.page { padding-top: var(--cap-native-navigation-top); padding-bottom: var(--cap-native-navigation-bottom);}可用变量:
--cap-native-navigation-top--cap-native-navigation-right--cap-native-navigation-bottom--cap-native-navigation-left--cap-native-navbar-height--cap-native-tabbar-height
图标描述
图标描述图标必须是可序列化的,因为原生 UI 会渲染它们。您可以使用跨平台 SVG、平台特定的 SVG、SF Symbols、捆绑的 iOS 图片、Android drawable 资源或捆绑的 Android 图片。
const icon = { svg: '<svg viewBox="0 0 24 24"><path d="M3 10.5 12 3l9 7.5"/></svg>', width: 24, height: 24, template: true, src: 'fallback_asset_name', ios: { svg: '<svg viewBox="0 0 24 24"><path d="M3 10.5 12 3l9 7.5"/></svg>', sfSymbol: 'house.fill', image: 'BundledAssetName', }, android: { svg: '<svg viewBox="0 0 24 24"><path d="M3 10.5 12 3l9 7.5"/></svg>', resource: 'ic_menu_view', image: 'bundled_drawable_name', },};内联 SVG 支持常见图标集(如 Lucide 和 Feather)使用的图标集焦点子集: path, line, polyline, polygon, circle, 和 rectSVG 图标默认以模板图像呈现,因此原生颜色可以重新颜色它们。
可选的 Web 组件
图标描述该包可以注册自定义元素以实现框架无关的声明性设置:
import { defineNativeNavigationElements } from '@capgo/capacitor-native-navigation';
defineNativeNavigationElements();<cap-native-navigation-provider enabled="true" content-inset-mode="css"></cap-native-navigation-provider>
<cap-native-navbar title="Home" transparent right-items='[{"id":"compose","title":"Compose","icon":{"svg":"<svg viewBox=\"0 0 24 24\"><path d=\"M12 20h9\"/></svg>"}}]'></cap-native-navbar>
<cap-native-tabbar selected-id="home" tabs='[{"id":"home","title":"Home","icon":{"ios":{"sfSymbol":"house.fill"}}}]'></cap-native-tabbar>平台说明
平台说明- iOS 渲染
UINavigationBar和UITabBar; iOS 26+ 使用系统 Liquid Glass 条件条行为。 - Android 渲染 AppCompat 工具栏和 Material 底部导航。
- Web 回退不绘制原生条。它反映事件和浏览器开发的 inset 变量。
- 插件保留一个全屏 Capacitor WebView。原生拥有框架、条、安全区域报告和过渡壳。
从 Getting Started 继续进行
继续从 Getting Started 开始如果您正在使用 Getting Started 来规划原生媒体和界面行为,连接它与 使用 @capgo/capacitor-native-navigation 为原生能力在使用 @capgo/capacitor-native-navigation 中 使用 @capgo/capacitor-live-activities 为原生能力在使用 @capgo/capacitor-live-activities 中 @capgo/capacitor-live-activities 为在 @capgo/capacitor-live-activities 中实现的细节 使用 @capgo/capacitor-video-player 为原生能力在使用 @capgo/capacitor-video-player 中 @capgo/capacitor-视频播放器 查看 @capgo/capacitor-视频播放器 的实现细节。