|
| 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. |
0 commit comments