Vue3 Slots

--

The usage in a Vue component:

<FancyButton>{{ message }}</FancyButton>

The component:

<button type="submit">
<slot>
Submit <!-- fallback content -->
</slot>
</button>

The “modelValue” placeholder is for the v-model attribute. It can be any other property, for example “title”:

<MyComponent v-model:title="bookTitle" />

Named slots

The usage in a Vue component:

<BaseLayout>
<template #header>
<h1>Here might be a page title</h1>
</template>

<template #default>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
</template>

<template #footer>
<p>Here's some contact info</p>
</template>
</BaseLayout>

The component:

<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>

Originally written on Mar 6, 2023.

--

--

Fabio Gollinucci
Fabio Gollinucci

Written by Fabio Gollinucci

Backend Developer & Cloud Architect @ Bitbull

No responses yet