In this tutorial, our goal is to understand how to use slots in Vue.js to create flexible layouts. Slots allow us to inject custom content from a parent component into a child component, enabling us to create and manage more complex UI structures easily.
By the end of this tutorial, you will learn:
- The concept of slots in Vue.js
- How to use slots to inject content into a component
- How to create flexible layouts using slots
Prerequisites: Basic knowledge of Vue.js, HTML, and JavaScript is required. Familiarity with Vue.js components will be beneficial.
Slots in Vue.js are a powerful tool that allows us to create flexible and reusable components. They allow us to define placeholders in a component that can be filled with any content we want when we use that component.
Think of slots as a way to "inject" content from a parent component into a child component. This makes your components more reusable and clean.
Here's a basic example:
// ChildComponent.vue
<template>
<div>
<slot></slot>
</div>
</template>
In this example, <slot></slot>
is where the parent component can inject any content it wants.
To use a slot, we can simply include it within the parent component's template when using the child component:
// ParentComponent.vue
<template>
<child-component>
<p>This is some slot content.</p>
</child-component>
</template>
The <p>
tag's content will replace the <slot>
in our child component. Hence, our child component will render as:
<div>
<p>This is some slot content.</p>
</div>
Now, let's look at some practical examples of using slots.
Here's an example of how to use the default slot in Vue.js:
ChildComponent.vue
<template>
<div>
<!-- Default slot -->
<slot></slot>
</div>
</template>
ParentComponent.vue
<template>
<child-component>
<!-- This content will be injected into the child component's slot -->
<h1>Hello, World!</h1>
</child-component>
</template>
In this example, the <h1>
tag's content from the parent will replace the <slot>
in our child component.
We can also use named slots to inject different content into different parts of the child component:
ChildComponent.vue
<template>
<div>
<header>
<!-- Named slot -->
<slot name="header"></slot>
</header>
<main>
<!-- Default slot -->
<slot></slot>
</main>
</div>
</template>
ParentComponent.vue
<template>
<child-component>
<h1 slot="header">This is the header.</h1>
<p>This is the main content.</p>
</child-component>
</template>
In this example, the <h1>
tag's content will replace the <slot name="header">
and the <p>
tag's content will replace the default <slot>
in the child component.
In this tutorial, we've learned about slots in Vue.js and how to use them to create flexible layouts. We've covered both default and named slots, and we've seen how we can inject custom content from a parent component into different parts of a child component.
For further learning, explore scoped slots in Vue.js, which allow you to pass data from the child component back out to the parent.
Solutions
1. Solution for Exercise 1
// ChildComponent.vue
<template>
<div>
<slot></slot>
</div>
</template>
// ParentComponent.vue
<template>
<child-component>
<p>This is some custom content.</p>
</child-component>
</template>
// ChildComponent.vue
<template>
<div>
<header>
<slot name="header"></slot>
</header>
<main>
<slot name="main"></slot>
</main>
</div>
</template>
// ParentComponent.vue
<template>
<child-component>
<h1 slot="header">This is the header.</h1>
<p slot="main">This is the main content.</p>
</child-component>
</template>