Skip to content

Commit 837a5e8

Browse files
skirtles-codeposva
andauthored
docs: rewrite the Named Routes page (#2176)
* docs: rewrite the Named Routes page * Update packages/docs/guide/essentials/named-routes.md [skip ci] --------- Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent cadcc47 commit 837a5e8

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

packages/docs/.vitepress/config/en.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
8484
text: "Routes' Matching Syntax",
8585
link: '/guide/essentials/route-matching-syntax.html',
8686
},
87+
{
88+
text: 'Named Routes',
89+
link: '/guide/essentials/named-routes.html',
90+
},
8791
{
8892
text: 'Nested Routes',
8993
link: '/guide/essentials/nested-routes.html',
@@ -92,10 +96,6 @@ export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
9296
text: 'Programmatic Navigation',
9397
link: '/guide/essentials/navigation.html',
9498
},
95-
{
96-
text: 'Named Routes',
97-
link: '/guide/essentials/named-routes.html',
98-
},
9999
{
100100
text: 'Named Views',
101101
link: '/guide/essentials/named-views.html',

packages/docs/guide/essentials/named-routes.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,41 @@
55
title="Learn about the named routes"
66
/>
77

8-
Alongside the `path`, you can provide a `name` to any route. This has the following advantages:
9-
10-
- No hardcoded URLs
11-
- Automatic encoding/decoding of `params`
12-
- Prevents you from having a typo in the url
13-
- Bypassing path ranking (e.g. to display a )
8+
When creating a route, we can optionally give the route a `name`:
149

1510
```js
1611
const routes = [
1712
{
1813
path: '/user/:username',
19-
name: 'user',
14+
name: 'profile', // [!code highlight]
2015
component: User
2116
}
2217
]
2318
```
2419

25-
To link to a named route, you can pass an object to the `router-link` component's `to` prop:
20+
We can then use the `name` instead of the `path` when passing the `to` prop to `<router-link>`:
2621

2722
```vue-html
28-
<router-link :to="{ name: 'user', params: { username: 'erina' }}">
29-
User
23+
<router-link :to="{ name: 'profile', params: { username: 'erina' } }">
24+
User profile
3025
</router-link>
3126
```
3227

33-
This is the exact same object used programmatically with `router.push()`:
28+
The example above would create a link to `/user/erina`.
3429

35-
```js
36-
router.push({ name: 'user', params: { username: 'erina' } })
37-
```
30+
- [See it in the Playground](https://play.vuejs.org/#eNqtVVtP2zAU/itWNqlFauNNIB6iUMEQEps0NjH2tOzBtKY1JLZlO6VTlP++4+PcelnFwyRofe7fubaKCiZk/GyjJBKFVsaRiswNZ45faU1q8mRUQUbrko8yuaPwlRfK/LkV1sHXpGHeq9JxMzScGmT19t5xkMaUaR1vOb9VBe+kntgWXz2Cs06O1LbCTwvRW7knGnEm50paRwIYcrEFd1xlkpBVyCQ5lN74ZOJV0Nom5JcnCFRCM7dKyIiOJkSygsNzBZiBmivAI7l0SUipRvuhCfPge7uWHBiGZPctS0iLJv7T2/YutFFPIt+JjgUJPn7DZ32CtWg7PIZ/4BASg7txKE6gC1VKNx69gw6NTqJJ1HQK5iR1vNA52M+8Yrr6OLuD+AuCtbQpBQYK9Oy6NAZAhLI1KKuKvEc69jSp65Tqw/oh3V7f00P9MsdveOWiecE75DDNhXwhiVMXWVRttYbUWdRpE2xOZ0sHxq1v2jl/a5jQyZ042Mv/HKjvt2aGFTCXFWmnAsTcCMkAxw4SHIjG9E2AUtpUusWyFvyVUGCltBsFmJB2W/dHZCHWswdYLwJ/XiulnrNr323zcQeodthDuAHTgmm4aEqCH1zsrBHYLIISheyyqD9Nnp1FK+e0TSgtpX5ZxrBBtNe4PItP4w8Q07oBN+a2mD4a9erPzDN4bzY1iy5BiS742imV2ynT4l8h9hQvz+Pz+COU/pGCdyrkgm/Qt3ddw/5Cms7CLXsSy50k/dJDT8037QTcuq1kWZ6r1y/Ic6bkHdD5is9fDvCf7SZA/m44ZLfmg+QcM0vugvjmxx3fwLsTFmpRwlwdE95zq/LSYwxqn0q5ANgDPUT7GXsm5PLB3mwcl7ZNygPFaqA+NvL6SOo93NP4bFDF9sfh+LThtgxvkF80fyxxy/Ac7U9i/RcYNWrd).
31+
32+
Using a `name` has various advantages:
3833

39-
In both cases, the router will navigate to the path `/user/erina`.
34+
- No hardcoded URLs.
35+
- Automatic encoding of `params`.
36+
- Avoids URL typos.
37+
- Bypassing path ranking, e.g. to display a lower-ranked route that matches the same path.
4038

41-
Full example [here](https://github.com/vuejs/vue-router/blob/dev/examples/named-routes/app.js).
39+
Each name **must be unique** across all routes. If you add the same name to multiple routes, the router will only keep the last one. You can read more about this [in the Dynamic Routing](../advanced/dynamic-routing#Removing-routes) section.
4240

43-
Each name **must be unique** across all routes. If you add the same name to multiple routes, the router will only keep the last one. You can read more about this [in the Dynamic Routing](../advanced/dynamic-routing.md#Removing-routes) section.
41+
There are various other parts of Vue Router that can be passed a location, e.g. the methods `router.push()` and `router.replace()`. We'll go into more detail about those methods in the guide to [programmatic navigation](./navigation). Just like the `to` prop, these methods also support passing a location by `name`:
42+
43+
```js
44+
router.push({ name: 'profile', params: { username: 'erina' } })
45+
```

0 commit comments

Comments
 (0)