参考 Next.js 官方文档,通过实际案例展示 7 种常用路由结构。点击卡片查看效果。
最基本的「目录→路径」映射
app/about/page.tsx → /about多级嵌套目录形成层级 URL
app/about/team/[slug] → /about/team/zhangsan用 (group) 括号分组,不影响 URL
app/(marketing)/pricing → /pricing用 [slug] 方括号接收路径参数
app/routing-demo/posts/[id]loading.tsx 显示骨架屏过渡
app/routing-demo/slow-page/loading.tsxerror.tsx 优雅捕获异常
app/routing-demo/buggy/error.tsxnot-found.tsx 自定义未找到页
app/routing-demo/articles/[id]/not-found.tsxapp/
├── layout.tsx # 根布局
├── page.tsx # 首页 /
├── (marketing)/ # 📦 路由组
│ └── pricing/page.tsx → /pricing
├── (shop)/ # 📦 路由组
│ └── products/page.tsx → /products
├── about/
│ ├── page.tsx → /about
│ ├── contact/page.tsx → /about/contact
│ └── team/
│ ├── page.tsx → /about/team
│ └── [slug]/page.tsx → /about/team/:slug
└── routing-demo/
├── layout.tsx # 实验室导航布局
├── page.tsx → /routing-demo
├── posts/
│ ├── page.tsx → /routing-demo/posts
│ └── [id]/page.tsx → /routing-demo/posts/:id
├── slow-page/
│ ├── page.tsx → /routing-demo/slow-page
│ └── loading.tsx # ⏳ 加载骨架屏
├── buggy/
│ ├── page.tsx → /routing-demo/buggy
│ └── error.tsx # 💥 错误边界
└── articles/
├── page.tsx → /routing-demo/articles
└── [id]/
├── page.tsx → /routing-demo/articles/:id
└── not-found.tsx # 🚫 自定义 404