Vue.js slots: default, named and scoped slots explained

Vue.js slots: default, named and scoped slots explained

Welcome to our complete guide to Vue.js slots. Vue.js is a popular JavaScript framework for building web applications, and it comes with plenty of features that make development easier and more efficient. Slots are one of them.

Slots are a powerful way of sharing content between components. They let you reuse pieces of HTML markup and drop them into other parts of the application.

What is a slot?

A slot is a special element in a Vue.js component that marks a place where outside content can be injected. It comes in handy when you want to reuse a component in several parts of your application, but with different content each time.

Say you have a component that renders a button. If you want to use that component in several places with a different label each time, a slot lets you inject the content that is specific to each use.

How do you use slots?

To use slots, you first have to declare one in the parent component. The syntax for declaring a slot looks like this:

javascript
<template>
  <div>
    <slot></slot>
  </div>
</template>

You can then use that component and inject content into the slot you declared:

javascript
<my-component>
  <p>Contenu injecté</p>
</my-component>

The injected content is rendered in place of the slot inside the component.

Types of slot

There are several types of slot to choose from, depending on what you need:

Default slot

This is the slot used when no other one is named. You declare it with the plain <slot></slot> syntax.

javascript
<template>
  <div>
    <slot></slot>
  </div>
</template>

Named slot

Named slots let you declare several slots in the same component and target each one by name. For example:

javascript
<template>
  <div>
    <slot name="header"></slot>
    <slot name="footer"></slot>
  </div>
</template>

Scoped slot

A scoped slot lets you inject content with a scope of its own. It is declared like this:

javascript
<template>
  <div>
    <slot v-bind:text="textProp"></slot>
  </div>
</template>

<script>
export default {
  data() {
    return {
      textProp: 'Texte par défaut'
    }
  }
}
</script>

When you use that component with a scoped slot, you can also pass properties through the slot:

javascript
<my-component>
  <template v-slot:default="slotProps">
    {{ slotProps.text }}
  </template>
</my-component>

Common use cases for slots

Slots are useful in plenty of situations, such as:

  • Reusing components with different content.
  • Customising components with specific styles and behaviours.
  • Building themes for components, so they can be used flexibly across the application.

Conclusion

Slots are a powerful Vue.js feature that lets you share content between components flexibly and efficiently. Different types of slot cover different needs, and they fit a wide range of situations.

We hope this detailed guide to Vue.js slots has been useful and has given you a good grasp of how they work and how to use them.

JavaScriptVue.js

Damien Flandrin Web developer since 2010, creator of Gekkode and Email Impact. Every article is tested on a real project before publication. Contact
Newsletter

New tests, tutorials and projects, by e-mail.

Reproducible tests, versioned code, dated results. Never any spam.