There are times when it’s useful to have multiple slots. For example, in a <base-layout>
component with the following template:
<div class="container">
<header>
<!-- We want header content here -->
</header>
<main>
<!-- We want main content here -->
</main>
<footer>
<!-- We want footer content here -->
</footer>
</div>
For these cases, the <slot>
element has a special attribute, name
, which can be used to define additional slots:
<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>
A <slot>
outlet without name
implicitly has the name “default”.
To provide content to named slots, we can use the v-slot
directive on a <template>
, providing the name of the slot as v-slot
‘s argument:
<base-layout>
<template v-slot:header>
<h1>Here might be a page title</h1>
</template>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
<template v-slot:footer>
<p>Here's some contact info</p>
</template>
</base-layout>
Now everything inside the <template>
elements will be passed to the corresponding slots. Any content not wrapped in a <template>
using v-slot
is assumed to be for the default slot.
However, you can still wrap default slot content in a <template>
if you wish to be explicit:
<base-layout>
<template v-slot:header>
<h1>Here might be a page title</h1>
</template>
<template v-slot:default>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
</template>
<template v-slot:footer>
<p>Here's some contact info</p>
</template>
</base-layout>