Expo Router 中,普通目录下的 index.tsx 的路由名称是 目录名/index,不是 目录名。只有用 (括号) 包裹的 group 目录才会做名称折叠。搞混这两者会导致 No route named "xxx" 警告。
规则
| 文件路径 | 路由名称 | URL 路径 |
|---|---|---|
app/index.tsx | index | / |
app/(tabs)/index.tsx | index | / |
app/(tabs)/history/index.tsx | history/index | /history |
app/(tabs)/settings/index.tsx | settings/index | /settings |
app/train/[module].tsx | [module] | /train/schulte |
关键区别:
(tabs)是 group 目录(括号语法)→ 名称被折叠,不出现在路由名和 URL 中history/是普通目录 → 路由名保留完整路径history/index
MindGym 中的实际 bug
Tab layout 中配置 Tab.Screen 时,错误地用了 name="history":
// ❌ 报警告: No route named "history"
<Tabs.Screen name="history" />
// ✅ 正确
<Tabs.Screen name="history/index" />
Expo Router 内部按文件路径注册路由,history/index.tsx 注册的名字就是 "history/index",用 "history" 找不到。
为什么用目录而不是扁平文件
history/index.tsx 而非 history.tsx 的好处是预留了二级页面扩展点——后续可以直接添加 history/[id].tsx(记录详情页),不需要重构目录结构。
参见
- Expo Router 深度解析 — 完整的文件路由规则
参考
- Expo Router: Groups
- MindGym
app/(tabs)/_layout.tsx— Tab.Screen name 配置