Skip to content

Commit c3f675c

Browse files
authored
docs: add content of concepts/routing (#196)
1 parent 4c57c89 commit c3f675c

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

content/2.concepts/3.routing/.template/files/pages/foo.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<template>
22
<div>
3-
Foo
3+
<div>
4+
Foo
5+
</div>
46
<div>
57
<NuxtLink to="/">
68
Index

content/2.concepts/3.routing/.template/files/pages/index.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<template>
22
<div>
3-
Hello!
3+
<div>
4+
Hello!
5+
</div>
46
<div>
57
<NuxtLink to="/foo">
68
Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- Challenge -->
2+
<template>
3+
TODO
4+
</template>

content/2.concepts/3.routing/index.md

+52-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,55 @@ ogImage: true
44

55
# Routing
66

7-
[Back to index](/)
7+
## File-based routing
8+
9+
The file-based routing is one of Nuxt’s core features.\
10+
Each Vue file inside the `pages/` directory corresponds to a URL (route) and renders its content.\
11+
For example, `pages/index.vue` corresponds to `/`, and `pages/foo.vue` corresponds to `/foo`.\
12+
This routing system is built on [vue-router](https://router.vuejs.org/).
13+
14+
Additionally, Nuxt optimizes each page through code splitting and other techniques, ensuring that only the minimal JavaScript required for the requested route is delivered.
15+
16+
## Navigation
17+
18+
Once you've created routes inside the `pages/` directory, you can use the `<NuxtLink>` component for navigation.
19+
20+
The `<NuxtLink>` component allows you to specify the route using the `to` prop to create links.\
21+
Compared to using an `<a>` tag with an `href` attribute, `<NuxtLink>` automatically optimizes navigation, resulting in faster page transitions.
22+
23+
## Route Parameters
24+
25+
Inside the `/pages` directory, you can define dynamic routes by wrapping part of the filename in `[]`.\
26+
For example: `pages/posts/[id].vue`.
27+
28+
The route parameter inside `[]` can be accessed via `useRoute()`.
29+
30+
```vue
31+
<!-- pages/posts/[id].vue -->
32+
<script setup lang="ts">
33+
const route = useRoute()
34+
35+
// When accessing /posts/1, route.params.id will be 1
36+
console.log(route.params.id)
37+
</script>
38+
```
39+
40+
## Challenge
41+
42+
Try implementing the `/posts/[id]` route and enabling navigation from `/` and `/foo` to `/posts/[id]`.\
43+
Additionally, in `/posts/[id]`, read the `id` from the route parameter and display it on the screen.
44+
45+
### Steps to complete:
46+
47+
1. In `pages/posts/[id].vue`, use `useRoute()` to get `id` from `params` and display it on the screen.
48+
2. In `pages/index.vue` and `pages/foo.vue`, use `<NuxtLink>` to create links that navigate to `/posts/[id]`.\
49+
For example, create a link that navigates to `/posts/1` (you can use any value).
50+
51+
If you get stuck, click the button below or the one in the top-right corner of the editor to view the solution.
52+
53+
:ButtonShowSolution{.bg-faded.px4.py2.rounded.border.border-base.hover:bg-active.hover:text-primary.hover:border-primary:50}
54+
55+
---
56+
57+
The `useRoute` function and `<NuxtLink>` component introduced in this section are automatically imported by Nuxt’s Auto Imports feature, so you don’t need to manually import them.\
58+
In the next section, we will explore [Auto Imports](/concepts/auto-imports) in more detail.

0 commit comments

Comments
 (0)