Skip to content

Commit 7c39e5f

Browse files
committed
docs: api/router-link
1 parent e5f0964 commit 7c39e5f

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

docs/en/api/router-link.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# `<router-link>`
2+
3+
`<router-link>` is the component for enabling user navigation in a router-enabled app. The target location is specified with the `to` prop. It renders as an `<a>` tag with correct `href` by default, but can be configured with the `tag` prop. In addition, the link automatically gets an active CSS class when the target route is active.
4+
5+
`<router-link>` is preferred over hard-coded `<a href="...">` for the following reasons:
6+
7+
- It works the same way in both HTML5 history mode and hash mode, so if you ever decide to switch mode, or when the router falls back to hash mode in IE9, nothing needs to be changed.
8+
9+
- In HTML5 history mode, `router-link` will intercept the click event so that the browser doesn't try to reload the page.
10+
11+
- When you are using the `base` option in HTML5 history mode, you don't need to include it in `to` prop's URLs.
12+
13+
#### Props
14+
15+
- **to**
16+
17+
- type: `string | Object`
18+
19+
- required
20+
21+
Denotes the target route of the link. When clicked, the value of the `to` prop will be passed to `router.push()` internally, so the value can be either a string or a location descriptor object.
22+
23+
``` html
24+
<!-- literal string -->
25+
<router-link to="home">Home</router-link>
26+
<!-- renders to -->
27+
<a href="home">Home</a>
28+
29+
<!-- javascript expression using v-bind -->
30+
<router-link v-bind:to="'home'">Home</router-link>
31+
32+
<!-- Omitting v-bind is fine, just as binding any other prop -->
33+
<router-link :to="'home'">Home</router-link>
34+
35+
<!-- same as above -->
36+
<router-link :to="{ path: 'home' }">Home</router-link>
37+
38+
<!-- named route -->
39+
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
40+
41+
<!-- with query, resulting in /register?plan=private -->
42+
<router-link :to="{ path: 'register', query: { plan: 'private' }}">Register</router-link>
43+
```
44+
45+
- **replace**
46+
47+
- type: `boolean`
48+
49+
- default: `false`
50+
51+
Setting `replace` prop will call `router.replace()` instead of `router.push()` when clicked, so the navigation will not leave a history record.
52+
53+
``` html
54+
<router-link :to="{ path: '/abc'}" replace></router-link>
55+
```
56+
57+
- **append**
58+
59+
- type: `boolean`
60+
61+
- default: `false`
62+
63+
Setting `append` prop always appends the relative path to the current path. For example, assuming we are navigating from `/a` to a relative link `b`, without `append` we will end up at `/b`, but with `append` we will end up at `/a/b`.
64+
65+
``` html
66+
<router-link :to="{ path: 'relative/path'}" append></router-link>
67+
```
68+
69+
- **tag**
70+
71+
- type: `string`
72+
73+
- default: `"a"`
74+
75+
Sometimes we want `<router-link>` to render as another tag, e.g `<li>`. Then we can use `tag` prop to specify which tag to render to, and it will still listen to click events for navigation.
76+
77+
``` html
78+
<router-link to="/foo" tag="li">foo</router-link>
79+
<!-- renders as -->
80+
<li>foo</li>
81+
```
82+
83+
- **active-class**
84+
85+
- type: `string`
86+
87+
- default: `"router-link-active"`
88+
89+
Configure the active CSS class applied when the link is active.
90+
91+
- **exact**
92+
93+
- type: `boolean`
94+
95+
- default: `false`
96+
97+
The default active class matching behavior is **inclusive match**. For example, `<router-link to="/a">` will get this class applied as long as the current path starts with `/a`.
98+
99+
One consequence of this is that `<router-link to="/">` will be active for every route! To force the link into "exact match mode", use the `exact` prop:
100+
101+
``` html
102+
<!-- this link will only be active at / -->
103+
<router-link to="/" exact>
104+
```
105+
106+
Checkout more examples explaining active link class [live](http://jsfiddle.net/fnlCtrl/dokbyypq/).
107+
108+
### Applying Active Class to Outer Element
109+
110+
Sometimes we may want the active class to be applied to an outer element rather than the `<a>` tag itself, in that case, you can render that outer element using `<router-link>` and wrap the raw `<a>` tag inside:
111+
112+
``` html
113+
<router-link tag="li" to="/foo">
114+
<a>/foo</a>
115+
</router-link>
116+
```
117+
118+
In this case the `<a>` will be the actual link (and will get the correct `href`), but the active class will be applied to the outer `<li>`.

0 commit comments

Comments
 (0)