动态路由与参数
学习实现动态路由,以处理 URL 中的可变片段并访问路由参数
动态路由与参数 是 CoddyKit 上的免费 Next.js 15 Fullstack (App Router + Server Actions) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack (App Router + Server Actions) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What are Dynamic Routes?
Imagine you have a blog with many posts. Instead of creating a separate page for each post (like /blog/post-1, /blog/post-2), you can use dynamic routes.
Dynamic routes allow you to create pages that respond to variable segments in the URL, making your application scalable and maintainable.
Defining a Basic Dynamic Route
In Next.js App Router, you define a dynamic route segment by wrapping a folder name in square brackets []. For example, to create a dynamic route for blog posts, you'd create a folder named [slug].
app/blog/[slug]/page.js
This structure will match URLs like /blog/first-post, /blog/another-article, etc.
Accessing Route Parameters
When a dynamic route is matched, the variable part of the URL becomes available as a route parameter. You can access these parameters within your page component via the params prop.
For app/blog/[slug]/page.js, the URL /blog/my-first-post would make params.slug equal to 'my-first-post'.
Demo: Simple Dynamic Page
Let's see how to create a simple dynamic page that displays the slug parameter from the URL. Place this code in app/blog/[slug]/page.js.
export default function BlogPostPage({ params }) {
// params.slug will contain the dynamic part of the URL
return (
<div>
<h1>Blog Post: {params.slug}</h1>
<p>This content is dynamically loaded!</p>
</div>
);
}Understanding Catch-all Segments
What if you need to match a path with an unknown number of segments? This is where catch-all segments come in handy. You define them using a spread syntax: [...slug].
app/docs/[...slug]/page.js
This will match URLs like /docs/a, /docs/a/b, /docs/a/b/c, etc.
Accessing Catch-all Parameters
When using a catch-all segment, the params prop will contain an array of the matched segments. So, for app/docs/[...slug]/page.js:
/docs/a->params.slugis['a']/docs/a/b/c->params.slugis['a', 'b', 'c']
Demo: Catch-all Page
Here's how you can display all segments from a catch-all route. Place this code in app/docs/[...slug]/page.js.
export default function DocsPage({ params }) {
// params.slug is an array of path segments
const path = params.slug.join('/');
return (
<div>
<h1>Documentation Path: /{path}</h1>
<p>Showing content for: {path}</p>
</div>
);
}Optional Catch-all Segments
Sometimes you want a catch-all segment that can also match the base path (i.e., zero segments). This is an optional catch-all, defined with double square brackets: [[...slug]].
app/users/[[...id]]/page.js
This will match /users (where params.id is undefined) AND /users/123 (where params.id is ['123']).
Demo: Optional Catch-all
This example shows an optional catch-all route that displays a user ID or 'Guest' if no ID is provided. Place this in app/users/[[...id]]/page.js.
export default function UserProfilePage({ params }) {
// params.id will be undefined for /users, or ['123'] for /users/123
const userId = params.id ? params.id[0] : 'Guest';
return (
<div>
<h1>Welcome, {userId}</h1>
<p>This page handles both base and dynamic paths.</p>
</div>
);
}Dynamic Routes Quiz
Which of the following file structures would allow a Next.js App Router application to handle URLs like /products/electronics/laptops and access 'electronics' and 'laptops' separately?
Recap: Dynamic Routes & Params
Great job! You've learned the essentials of dynamic routing in Next.js App Router:
[slug]creates a single dynamic segment.[...slug]creates a catch-all segment for multiple paths, returning an array.[[...slug]]creates an optional catch-all, matching zero or more segments.- All dynamic segments are accessed via the
paramsprop in yourpage.jscomponents.
This powerful feature is key for building flexible and scalable web applications!
常见问题解答
「动态路由与参数」课时是免费的吗?
是的 — 「动态路由与参数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack (App Router + Server Actions) 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack (App Router + Server Actions) 课程共包含 4 节课。
「动态路由与参数」这节课中我会学到什么?
学习实现动态路由,以处理 URL 中的可变片段并访问路由参数 你通过在浏览器中直接运行的动手代码来练习 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) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「动态路由与参数」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Next.js 15 Fullstack (App Router + Server Actions) 课中编写并运行代码吗?
能。每节 Next.js 15 Fullstack (App Router + Server Actions) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 布局与页面组件
- 动态路由与参数
- 加载与错误用户界面
- 路由组与嵌套路由布局