Dynamische Routen und Parameter
Lernen Sie, dynamische Routen zu implementieren, um variable URL-Segmente zu verarbeiten und auf Routenparameter zuzugreifen.
Dynamische Routen und Parameter ist eine kostenlose Next.js 15 Fullstack (App Router + Server Actions)-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Next.js 15 Fullstack (App Router + Server Actions)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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!
Häufig gestellte Fragen
Ist die Lektion „Dynamische Routen und Parameter“ kostenlos?
Ja — der vollständige Text von „Dynamische Routen und Parameter“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Next.js 15 Fullstack (App Router + Server Actions)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Dynamische Routen und Parameter“?
Lernen Sie, dynamische Routen zu implementieren, um variable URL-Segmente zu verarbeiten und auf Routenparameter zuzugreifen. Du übst Next.js 15 Fullstack (App Router + Server Actions) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Next.js 15 Fullstack (App Router + Server Actions) zu starten?
Keine Vorkenntnisse erforderlich. Next.js 15 Fullstack (App Router + Server Actions) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Dynamische Routen und Parameter“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Next.js 15 Fullstack (App Router + Server Actions)-Lektion Code schreiben und ausführen?
Ja. Jede Next.js 15 Fullstack (App Router + Server Actions)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Layouts und Seitenkomponenten
- Dynamische Routen und Parameter
- Lade- und Fehler-UI
- Routengruppen und verschachtelte Layouts