React Native Expo Vibe Coding:路由與權限配置範本
基於 Expo Router 與 EAS 的跨端 App 開發範本,實現類型安全與原生權限自動化配置。

量化可完成度
整體完成度為 83%,AI 在生成路由結構與類型定義方面表現優異,但在處理複雜的原生權限衝突或特定設備適配時可能需要人工介入。
四維達成明細
文件路由規則清晰,AI 能準確生成動態路由與分組結構,減少手動配置錯誤。
自動套用 userInterfaceStyle: automatic,但安全區(SafeArea)處理需結合 expo-secure-store 或特定 UI 庫驗證。
權限聲明在 config 中集中管理,但執行時權限請求邏輯需額外程式碼實現,AI 易遺漏動態檢查。
EAS 配置標準化程度高,但不同平台的簽名憑證管理仍需人工介入以確保安全。
人工把關檢查點
開發者必須手動審核 app.config.ts 中的權限描述文案是否符合 App Store/Google Play 審核政策,並確認 EAS projectId 配置正確。
升級 / 降級路徑
若預算有限,可降級為本地建置或使用 Expo Go 進行開發;若需更高級的 CI/CD,可整合 GitHub Actions 觸發 EAS Build。
適用場景與前提
適用於希望快速搭建跨端移動應用的開發者,需具備 Node.js 環境與 Expo CLI。理想場景包括需要頻繁迭代 UI 介面、處理原生權限(如相機)以及依賴 CI/CD 自動建置的團隊專案。
可直接複製的工程設定
定义 Cursor AI 在 Expo Router 文件路由开发中的核心规则,确保类型安全与目录结构规范。
---
description: Expo Router file-based routing rules for React Native
alwaysApply: true
---
# Expo Router Rules
1. Use `app/` directory for all routes. Do not use manual navigation stacks.
2. Dynamic routes must be named `[slug].tsx` or `(group)/[id].tsx`.
3. Always import `expo-router` components (`Stack`, `Tabs`, `Link`) instead of `react-navigation` directly.
4. Ensure TypeScript strict mode is enabled in `tsconfig.json`.
5. Handle deep linking via `scheme` in `app.config.ts` and use `useRouter()` for programmatic navigation.
6. Group routes using parentheses `(group)` to hide them from URL structure but keep them in the stack.配置 Expo 应用元数据、原生权限声明及 EAS 构建环境变量,确保 iOS/Android 适配与商店审核合规。
import { ExpoConfig, ConfigContext } from 'expo/config';
export default ({ config }: ConfigContext): ExpoConfig => ({
...config,
name: "VibeApp",
slug: "vibe-app",
scheme: "vibeapp",
version: "1.0.0",
orientation: "portrait",
icon: "./assets/icon.png",
userInterfaceStyle: "automatic",
splash: {
image: "./assets/splash.png",
resizeMode: "contain",
backgroundColor: "#ffffff"
},
assetBundlePrefix: "vibeapp",
plugins: [
["expo-camera", { cameraPermission: "用于拍摄内容" }],
["expo-notifications", { androidMode: "transparent" }]
],
extra: {
eas: {
projectId: "YOUR_PROJECT_ID"
}
},
ios: {
bundleIdentifier: "com.example.vibeapp",
supportsTablet: true,
infoPlist: {
NSCameraUsageDescription: "需要访问相机以拍摄照片",
NSPhotoLibraryUsageDescription: "需要访问相册以选择图片"
}
},
android: {
package: "com.example.vibeapp",
permissions: ["CAMERA", "NOTIFICATIONS"]
}
});AI Vibe Coding 四步實戰
- 1
初始化 Expo Router 專案結構
目標: 建立符合文件路由規範的目錄骨架,確保 TypeScript 類型安全。
PromptCreate a new Expo project with Expo Router. Ensure `app/` directory exists. Add a `(tabs)` group with `home.tsx` and `settings.tsx`. Use strict TypeScript types for route params.驗收標準:
Run `npx expo start` and verify that the router generates correct links in `expo-router` dev tools. - 2
配置原生權限與主題
目標: 在 app.config.ts 中聲明相機權限並啟用自動深淺色模式。
PromptUpdate app.config.ts to include `expo-camera` plugin with permission string. Set `userInterfaceStyle` to 'automatic'. Ensure iOS Info.plist keys are generated correctly.驗收標準:
Run `npx expo prebuild` and check `ios/Info.plist` for NSCameraUsageDescription. - 3
實現安全區與手勢導航
目標: 加入 SafeAreaProvider 和 Gesture Handler,確保 UI 在瀏海螢幕上正常顯示。
PromptAdd `expo-secure-store` and `react-native-gesture-handler`. Wrap the root layout in `SafeAreaProvider`. Implement a simple swipe gesture on the home screen.驗收標準:
Run the app on a simulator with notch and verify content does not overlap system bars. - 4
配置 EAS Build 與審核
目標: 設定 EAS projectId 並生成建置配置檔案,準備雲端建置。
PromptInitialize EAS project. Create `eas.json` with development, preview, and production profiles. Ensure `app.config.ts` extra.eas.projectId is set.驗收標準:
Run `npx eas build --profile development --platform ios` (dry run) to check config validity.
常見踩坑與解法
- Expo Router 動態路由命名錯誤導致類型推斷失敗:確保使用 `[slug].tsx` 而非 `slug.tsx`。
- 原生權限在開發模式與生產模式行為不一致:需在程式碼中顯式處理 `expo-permissions` 或 `expo-camera` 的執行時請求。
- EAS 建置快取導致舊配置生效:修改 app.config.ts 後需執行 `npx expo prebuild --clean` 以重新生成原生工程。
常見問題
Expo Router 如何處理巢狀路由?
使用括號 `(group)` 建立邏輯分組,這些目錄不會出現在 URL 中但會保留在導航堆疊中。動態參數透過 `[param].tsx` 定義。
為什麼 app.config.ts 中的權限描述在 iOS 上無效?
iOS 權限描述必須寫入 Info.plist。Expo 會自動從 config 生成,但需確保執行 `expo prebuild` 或重新安裝應用程式以刷新原生層配置。
EAS Build 失敗時如何除錯?
檢查 `eas.json` 中的 profile 設定和 `app.config.ts` 中的 projectId。查看 EAS 主控台日誌,通常錯誤源於簽名憑證缺失或相依版本衝突。


