页面、布局与路由基础
探索 App Router,定义页面、创建共享布局,并在应用程序的各个路由之间导航。
页面、布局与路由基础 是 CoddyKit 上的免费 Next.js 15 Fullstack Web Apps 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack Web Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome to App Router
The App Router is Next.js 15's routing system, built on React Server Components. It lets you co-locate components, styles, and tests by route.
Routes from Your Files
Routing is filesystem-based: your folder structure inside app/ directly defines your URLs, keeping the project predictable.
Defining a Page
A page.js file inside a folder makes that route render. It exports the React component shown when a user visits that URL.
Code: Simple Page
Create app/page.js and it automatically becomes your home page at the / route. Here is the component that renders there.
export default function HomePage() {
return (
<div>
<h1>Welcome to CoddyKit!</h1>
<p>This is your Next.js home page.</p>
</div>
);
}Organizing with Nested Routes
Build nested routes by nesting folders in app/ — each folder is a URL segment, so app/dashboard/settings/ maps to /dashboard/settings.
Code: Nested Page
To serve /dashboard/settings, create that folder path with a page.js inside. The file's component renders at that URL.
// app/dashboard/settings/page.js
export default function DashboardSettingsPage() {
return (
<div>
<h2>Dashboard Settings</h2>
<p>Manage your preferences here.</p>
</div>
);
}Shared UI with Layouts
A layout (layout.js) is shared UI that wraps the pages in its segment — ideal for navbars, sidebars, and footers via the children prop.
Code: Root Layout
Every App Router project needs a root layout.js. It defines the base HTML — the <html> and <body> tags around your whole app.
// app/layout.js
import './globals.css'; // Global styles (optional)
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>Home | About</nav>
</header>
<main>{children}</main> {/* Page content goes here */}
<footer>© 2024 CoddyKit</footer>
</body>
</html>
);
}Nested Layouts
Nested layouts apply only to their folder and subfolders, inheriting from parent layouts — so you can build hierarchical UI per section.
Linking Between Pages
Navigate with the <Link> component from next/link. It behaves like an anchor but does client-side routing — no full page reload.
// app/page.js (example usage)
import Link from 'next/link';
export default function HomePage() {
return (
<div>
<h1>Welcome!</h1>
<p>Go to the <Link href="/about">About Us</Link> page.</p>
<p>Or visit your <Link href="/dashboard">Dashboard</Link>.</p>
</div>
);
}Routing & Layouts Check
Which of the following statements about Next.js 15 App Router are TRUE?
Recap: Pages, Layouts & Routing
You learned the App Router: folders define routes, page.js renders content, layout.js shares UI, and <Link> handles fast navigation.
常见问题解答
「页面、布局与路由基础」课时是免费的吗?
是的 — 「页面、布局与路由基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack Web Apps 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。
「页面、布局与路由基础」这节课中我会学到什么?
探索 App Router,定义页面、创建共享布局,并在应用程序的各个路由之间导航。 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack Web Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Next.js 15 Fullstack Web Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack Web Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「页面、布局与路由基础」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Next.js 15 Fullstack Web Apps 课中编写并运行代码吗?
能。每节 Next.js 15 Fullstack Web Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Next.js 15 简介
- 项目设置与配置
- 页面、布局与路由基础
- 元数据、SEO 与文档头部