|
5 | 5 | title="Learn about the named routes"
|
6 | 6 | />
|
7 | 7 |
|
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`: |
14 | 9 |
|
15 | 10 | ```js
|
16 | 11 | const routes = [
|
17 | 12 | {
|
18 | 13 | path: '/user/:username',
|
19 |
| - name: 'user', |
| 14 | + name: 'profile', // [!code highlight] |
20 | 15 | component: User
|
21 | 16 | }
|
22 | 17 | ]
|
23 | 18 | ```
|
24 | 19 |
|
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>`: |
26 | 21 |
|
27 | 22 | ```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 |
30 | 25 | </router-link>
|
31 | 26 | ```
|
32 | 27 |
|
33 |
| -This is the exact same object used programmatically with `router.push()`: |
| 28 | +The example above would create a link to `/user/erina`. |
34 | 29 |
|
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: |
38 | 33 |
|
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. |
40 | 38 |
|
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. |
42 | 40 |
|
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