0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · 课时

路由组与嵌套路由布局

使用路由组、嵌套路由布局和模板文件,组织包含共享界面及分区界面的复杂 App Router 项目。

路由组与嵌套路由布局 是 CoddyKit 上的免费 Next.js 15 Fullstack (App Router + Server Actions) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack (App Router + Server Actions) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Organization Matters

As an app grows, the App Router lets you structure folders for shared UI without affecting the URL. Route groups and nested layouts are the main tools.

Nested Layouts Recap

Each folder can have a layout.tsx. Layouts nest: a section layout wraps its pages while the root layout wraps everything.

export default function DashboardLayout({ children }) {
  return (<section><nav>Dashboard nav</nav>{children}</section>);
}

Route Groups

Wrapping a folder name in parentheses, like (marketing), creates a route group. It organizes files but is omitted from the URL.

Multiple Root Layouts

Route groups let you have different top-level layouts — for example, one for marketing pages and another for the app — by giving each group its own layout.

app/
  (marketing)/layout.tsx
  (marketing)/page.tsx
  (app)/layout.tsx
  (app)/dashboard/page.tsx

URLs Stay Clean

Because (marketing) does not appear in the path, app/(marketing)/page.tsx still serves the URL /, not /marketing.

Template vs Layout

A template.tsx is like a layout but creates a new instance on every navigation, resetting state — useful for enter animations or per-route effects.

export default function Template({ children }) {
  return <div className="fade-in">{children}</div>;
}

When to Use Template

Use a template when you need state or effects to re-run on each navigation; use a layout when you want shared UI and state to persist across navigations.

Organizing Without Routing

You can also use route groups purely to colocate related routes under one folder without adding a URL segment, keeping the codebase tidy.

Shared UI in Layouts

Put navigation, sidebars, and providers in the nearest layout so they persist and avoid re-rendering on navigation within that section.

Loading and Error per Section

Each segment, including grouped ones, can have its own loading.tsx and error.tsx, scoping fallback UI precisely.

Putting It Together

A typical app: a (marketing) group with a public layout, an (app) group with an authenticated layout, each nesting further section layouts.

Quick Check

What does wrapping a folder name in parentheses do?

Recap

You learned to structure apps with nested layouts for persistent shared UI, route groups (name) that organize without changing URLs and allow multiple root layouts, and templates that remount on navigation.

常见问题解答

「路由组与嵌套路由布局」课时是免费的吗?

是的 — 「路由组与嵌套路由布局」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack (App Router + Server Actions) 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。

「路由组与嵌套路由布局」这节课中我会学到什么?

使用路由组、嵌套路由布局和模板文件,组织包含共享界面及分区界面的复杂 App Router 项目。 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack (App Router + Server Actions),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Next.js 15 Fullstack (App Router + Server Actions) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack (App Router + Server Actions) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「路由组与嵌套路由布局」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Next.js 15 Fullstack (App Router + Server Actions) 课中编写并运行代码吗?

能。每节 Next.js 15 Fullstack (App Router + Server Actions) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 布局与页面组件
  2. 动态路由与参数
  3. 加载与错误用户界面
  4. 路由组与嵌套路由布局
← 返回 Next.js 15 Fullstack (App Router + Server Actions)