Pre-order my new book on Vue Design Patterns at 30% discount. Click here for more details

Slots in Vue 3 Deep Dive

Published by Solomon Eseme's profile picture Solomon Eseme on  Mon, 29 May 2023 19:30:00 GMT

Slots are a powerful feature in Vue 3 that allows developers to create flexible and reusable components with customizable content. They provide a way to pass content from a parent component to its child components, enabling greater component composition and reusability. In this article, we will explore slots in Vue 3, discuss the different types of slots, and demonstrate how to use them effectively in your Vue applications.

Understanding Slots in Vue 3

Slots provide a mechanism for creating component templates that can receive content from the parent component. With slots, you can define placeholders in your component's template where the parent component can inject its content. This enables the parent component to customize the appearance and behavior of the child component without modifying its internal structure.

Slots in Vue 3 are more flexible and powerful than in previous versions of Vue. They can be named, scoped, or even dynamic, allowing for more advanced component composition. Let's dive into each type of slot and see how they can be used.

Default Slots

The default slot, also known as the unnamed slot, is the simplest type of slot. It allows the parent component to pass content directly into the component's template. Here's an example:

<!-- ChildComponent.vue -->
<template>
<div>
<slot></slot>
</div>
</template>
<!-- ParentComponent.vue -->
<template>
<ChildComponent>
<p>This content will be injected into the slot.</p>
</ChildComponent>
</template>

In this example, the ChildComponent defines a default slot using the <slot></slot> element. The content passed between the opening and closing tags of the ChildComponent will be rendered in place of the slot.

Named Slots

Named slots, also known as scoped slots, provide a way to have multiple slots within a component and give them distinct names. This allows the parent component to selectively inject content into specific slots. Here's an example:

<!-- ChildComponent.vue -->
<template>
<div>
<slot name="header"></slot>
<slot></slot>
<slot name="footer"></slot>
</div>
</template>
<!-- ParentComponent.vue -->
<template>
<ChildComponent>
<template v-slot:header>
<h1>Header content</h1>
</template>

<p>This content will be injected into the default slot.</p>

<template v-slot:footer>
<footer>Footer content</footer>
</template>
</ChildComponent>
</template>

In this example, the ChildComponent defines three slots: a named slot with the name "header", a default slot, and another named slot with the name "footer". The parent component then uses the v-slot directive to pass content to the respective slots.

Dynamic Slots

Dynamic slots take slot usage to the next level by allowing the parent component to dynamically select which slot to use based on data or conditions. This provides even greater flexibility in component composition. Let's see an example:

<!-- ChildComponent.vue -->
<template>
<div>
<slot :name="slotName"></slot>
</div>
</template>

<script>
import { ref } from "vue";

export default {
setup() {
const slotName = ref("");

return {
slotName,
};
},
};
</script>
<!-- ParentComponent.vue -->
<template>
<ChildComponent>
<template v-if="condition" v-slot:header>
<h1>Header content</h1>
</template>

<template v-else v-slot:footer>
<footer>Footer content</footer>
</template>
</ChildComponent>
</template>

<script>
import { ref } from "vue";

export default {
setup() {
const condition = ref(true);

return {
condition,
};
},
};
</script>

In this example, the ChildComponent uses a dynamic slot by binding the name attribute of the <slot></slot> element to a variable called slotName. The parent component then uses the v-slot directive with conditional rendering to decide which slot to use based on the condition variable.

Scoped Slots

Scoped slots take slots in Vue 3 a step further by allowing data to be passed from the child component to the parent component. This enables more dynamic and flexible composition of components. Scoped slots are defined using the <template> tag with the v-slot directive and can receive data from the child component. Let's illustrate this with an example:

<!-- ChildComponent.vue -->
<template>
<div>
<slot :data="childData"></slot>
</div>
</template>

<script>
export default {
data() {
return {
childData: "Child Data",
};
},
};
</script>

In this example, the ChildComponent has a slot that receives the childData as a prop. Now, when we use the ChildComponent, we can access the childData in the parent component:

<!-- ParentComponent.vue -->
<template>
<ChildComponent>
<template v-slot="{ data }">
<p></p>
</template>
</ChildComponent>
</template>

By using the v-slot directive, we can capture the childData in the parent component and use it within the slot's content.

Learning and Using Slots in Vue 3

To learn more about slots in Vue 3 and master their usage, here are some recommended resources:

By studying these resources, you will gain a comprehensive understanding of slots in Vue 3, the different types of slots, and how to leverage them effectively in your Vue applications.

Conclusion

Slots in Vue 3 provide a powerful mechanism for creating flexible and reusable components. By allowing the parent component to inject content into the child component's template, slots enable greater component composition and customization. Whether you need to pass simple content or create complex dynamic compositions, Vue 3's slot system has got you covered.

In this article, we explored the concept of slots in Vue 3, covering default slots, named slots, and dynamic slots. We saw how each type of slot can be used to create customizable components and enhance component reusability.

Remember to refer to the official Vue 3 documentation and additional learning resources for further exploration and mastery of slots in Vue 3.

Resources

Architecting Vue.js 3 Enterprise-Ready Web Applications

Get the FREE chapter!

With this 15+ pages long chapter You will learn how to unit test a Vue.js 3 component and the components and pages’ methods: You will also learn Unit Testing tools such as Jest and Mocha and use them to effectively unit Test an enterprise project.

    Architecting Vue.js 3 Enterprise-Ready Web Applications chapter 9 Best Practice in Unit Testing