Skip to content

Commit b8cc3de

Browse files
docs: add a guide to the RouterView slot (#2049)
* docs: add a guide to the RouterView slot * docs: updates to router-view-slot.md based on review feedback
1 parent 3356ef4 commit b8cc3de

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ export const enConfig: LocaleSpecificConfig<DefaultTheme.Config> = {
134134
text: 'Composition API',
135135
link: '/guide/advanced/composition-api.html',
136136
},
137+
{
138+
text: 'RouterView slot',
139+
link: '/guide/advanced/router-view-slot.html',
140+
},
137141
{
138142
text: 'Transitions',
139143
link: '/guide/advanced/transitions.html',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# RouterView slot
2+
3+
The RouterView component exposes a slot that can be used to render the route component:
4+
5+
```vue-html
6+
<router-view v-slot="{ Component }">
7+
<component :is="Component" />
8+
</router-view>
9+
```
10+
11+
The code above is equivalent to using `<router-view />` without the slot, but the slot provides extra flexibility when we want to work with other features.
12+
13+
## KeepAlive & Transition
14+
15+
When working with the [KeepAlive](https://vuejs.org/guide/built-ins/keep-alive.html) component, we would usually want it to keep the route components alive, not the RouterView itself. We can achieve that by putting the KeepAlive inside the slot:
16+
17+
```vue-html
18+
<router-view v-slot="{ Component }">
19+
<keep-alive>
20+
<component :is="Component" />
21+
</keep-alive>
22+
</router-view>
23+
```
24+
25+
Similarly, the slot allows us to use a [Transition](https://vuejs.org/guide/built-ins/transition.html) component to transition between route components:
26+
27+
```vue-html
28+
<router-view v-slot="{ Component }">
29+
<transition>
30+
<component :is="Component" />
31+
</transition>
32+
</router-view>
33+
```
34+
35+
We can also use KeepAlive inside a Transition:
36+
37+
```vue-html
38+
<router-view v-slot="{ Component }">
39+
<transition>
40+
<keep-alive>
41+
<component :is="Component" />
42+
</keep-alive>
43+
</transition>
44+
</router-view>
45+
```
46+
47+
For more information about using RouterView with the Transition component, see the [Transitions](./transitions) guide.
48+
49+
## Passing props and slots
50+
51+
We can use the slot to pass props or slots to the route component:
52+
53+
```vue-html
54+
<router-view v-slot="{ Component }">
55+
<component :is="Component" some-prop="a value">
56+
<p>Some slotted content</p>
57+
</component>
58+
</router-view>
59+
```
60+
61+
In practice, this usually isn't something you would want to do, as the route components would **all need to use the same props and slots**. See [Passing Props to Route Components](../essentials/passing-props) for other ways to pass props.
62+
63+
## Template refs
64+
65+
Using the slot allows us to put a [template ref](https://vuejs.org/guide/essentials/template-refs.html) directly on the route component:
66+
67+
```vue-html
68+
<router-view v-slot="{ Component }">
69+
<component :is="Component" ref="mainContent" />
70+
</router-view>
71+
```
72+
73+
If we put the ref on the `<router-view>` instead then the ref would be populated with the RouterView instance, rather than the route component.

packages/docs/guide/advanced/transitions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
title="Learn about route transitions"
66
/>
77

8-
In order to use transitions on your route components and animate navigations, you need to use the v-slot API:
8+
In order to use transitions on your route components and animate navigations, you need to use the [`<RouterView>` slot](./router-view-slot):
99

1010
```html
1111
<router-view v-slot="{ Component }">

packages/docs/guide/essentials/passing-props.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Try to keep the `props` function stateless, as it's only evaluated on route chan
8181

8282
## Via RouterView
8383

84-
You can also pass any props directly via `<RouterView>`:
84+
You can also pass any props via the [`<RouterView>` slot](../advanced/router-view-slot):
8585

8686
```vue-html
8787
<RouterView v-slot="{ Component }">

0 commit comments

Comments
 (0)