Skip to content

Commit b43c576

Browse files
authored
docs(link): add v-slot for router-link (#2870)
* docs(link): add v-slot for router-link * docs: move to one single line * docs: unify in one line [ci skip]
1 parent 5ad26bc commit b43c576

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

docs/api/README.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,52 @@ sidebar: auto
1313
`<router-link>` is preferred over hard-coded `<a href="...">` for the following reasons:
1414

1515
- 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.
16-
1716
- In HTML5 history mode, `router-link` will intercept the click event so that the browser doesn't try to reload the page.
18-
1917
- When you are using the `base` option in HTML5 history mode, you don't need to include it in `to` prop's URLs.
2018

21-
### Applying Active Class to Outer Element
19+
### `v-slot` API (+3.1.0)
20+
21+
`router-link` exposes a low level customization through a [scoped slot](https://vuejs.org/v2/guide/components-slots.html#Scoped-Slots). This is a more advanced API that primarily targets library authors but can come in handy for developers as well, most of the time in a custom component like a _NavLink_ or other.
22+
23+
**When using the `v-slot` API, it is required to pass one single child to `router-link`**. If you don't, `router-link` will wrap its children in a `span` element.
24+
25+
```html
26+
<router-link
27+
to="/about"
28+
v-slot="{ href, route, navigate, isActive, isExactActive }"
29+
>
30+
<NavLink :active="isActive" :href="href" @click="navigate"
31+
>{{ route.fullPath }}</NavLink
32+
>
33+
</router-link>
34+
```
35+
36+
- `href`: resolved url. This would be the `href` attribute of an `a` element
37+
- `route`: resolved normalized location
38+
- `navigate`: function to trigger the navigation. **It will automatically prevent events when necessary**, the same way `router-link` does
39+
- `isActive`: `true` if the [active class](#active-class) should be applied. Allows to apply an arbitrary class
40+
- `isExactActive`: `true` if the [exact active class](#exact-active-class) should be applied. Allows to apply an arbitrary class
41+
42+
#### Example: Applying Active Class to Outer Element
2243

23-
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:
44+
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 wrap that element inside a `router-link` and use the `v-slot` properties to create your link:
2445

2546
```html
26-
<router-link tag="li" to="/foo">
27-
<a>/foo</a>
47+
<router-link
48+
to="/foo"
49+
v-slot="{ href, route, navigate, isActive, isExactActive }"
50+
>
51+
<li
52+
:class="[isActive && 'router-link-active', isExactActive && 'router-link-exact-active']"
53+
>
54+
<a :href="href" @click="navigate">{{ route.fullPath }}</a>
55+
</li>
2856
</router-link>
2957
```
3058

31-
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>`.
59+
:::tip
60+
If you add a `target="_blank"` to your `a` element, you must omit the `@click="navigate"` handler.
61+
:::
3262

3363
## `<router-link>` Props
3464

0 commit comments

Comments
 (0)