loading...
Cover image for Understanding Slot in Vue.js

Understanding Slot in Vue.js

suniljoshi19 profile image Sunil Joshi Updated on ・3 min read

In this article, we will get a full understanding of slot through practical application of it various use cases.

What is slot

Slots are reserved space offered by vuejs to display content passed down from one component to another. There are two types of slot in vuejs namely: named slot and unnamed(default) slot.

Practical Use Case

• To pass down Html elements from one component to another.

With props, vue allows us to pass strings, objects, arrays and functions from a parent component to its child component. While it is possible for us to pass html elements from a parent to it child component as string this will make our website vulnerable to cross site scripting attack that is why vuejs provides us with slot which is a more secure and reliable method of passing html element and other contents from parent to its child component for rendering.

How to Use Slot

In the child component where the content is to be displayed, add the slot tag as follows:

In this tutorial we will generate our project with the vue CLI

vue create slot-project

In the src folder create a components folder with parent.vue and child.vue files

Add the code below to child.vue

<template>
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
               <h2>Child Component</h2>
            </div>
        </div>
    </div>
</template>

<script>
    export default {

    }
</script>
<style>
</style>
Add the code snippet below to parent.vue

<template>
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
               <h2>Parent</h2>
               <child-component>
                    <h3 class="child-content">You are my first child</h3>
                    <h4 class="child-content">Your name is Tom</h4>
               </child-component>
            </div>
        </div>
    </div>
</template>

<script>
import child from './child.vue'
    export default {
        components: {
            child-component: child
        }
    }
</script>

<style>
</style>

Here we imported the child component and pass down the html content to the child.

For these contents to be displayed in the child component, the slot tag must be added to the child component.

Add the slot tag to the child.vue file as follow:

<template>
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
               <h2>Child Component</h2>
               <slot></slot>
            </div>
        </div>
    </div>
</template>

<script>

    export default {

    }
</script>

<style>
</style>

In app.js file add the parent.vue component

<template>
    <div class="app">
        <Parent/>
    </div>
</template>

<script>
import Parent from './components/Parent'
    export default {
      components: {
          Parent
      }  
    }
</script>

<style>
</style>

Now, we can verify that slot is working as expected.

npm run serve

Now our app should be like:

Styling Slot Component

For styling our slot component, the css styles should be added to the component with the slot tag.

So in child.vue component we will add the following styles to the html content received from the parent.vue component.

<style>
.child-content {
    background-color: blue;
    color: red;
}
</style>

Using Multiple Slots

In order to use multiple slots in vue, vuejs provides us with a way to name our slots.

What if we want the h2 and h3 tags in the parent component to be displayed individually in separate slots. This would be a typical use case for named slots.

In the Parent.vue component we will name our slots as follows:

<template>
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
               <h2>Parent</h2>
               <child-component>
                    <h3 class="child-content" slot="message">You are my first child</h3>
                    <h4 class="child-content" slot="name">Your name is Tom</h4>
               </child-component>
            </div>
        </div>
    </div>
</template>

In the child.vue component we will recieve the named slot as follows:

<template>
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
               <h2>Child Component</h2>
               <slot name="message"></slot>
               <slot name="name"></slot>
            </div>
        </div>
    </div>
</template>

Here vuejs reserves two slots to display the content of the slot attribute with the value of message and name as separate contents.

Conclusion

In this article we have seen a practical use case of slots to transfer content from a parent component to a child component. For more information on slot, check out the Vue documentation.

Bonus Tips for vuejs developers and agencies : Use ready to use and stunning vue templates to complete your web application faster and smoother. Lots of them are free to download and create custom projects. So do check them out.

Posted on by:

suniljoshi19 profile

Sunil Joshi

@suniljoshi19

An avid designer cum developer who is passionate about solving complex UX challenges across digital businesses.

Discussion

markdown guide
 

I recently built a WebComponent using VueJS to apply spaced-repetition technique to flashcards. Essentially, I wanted two custom elements: and where the multiple instances of the can be nested inside . A demo is here: nilesh.trivedi.pw/articles/spacedr...

While slots worked fine when I used these as Vue components, I couldn't get them to work in the WebComponent/UMD build. This is because I didn't want the end-user of this library to have to use npm or any bundlers. I used vue-cli-service build --target lib --name CardSet components/CardSet.vue to generated these.

I think for Webcomponents still need more documentation.

 

Hey, If you want to add WebComponent/UMD into your application you have to configure your Vue CLI a little further. Vue CLI has done some explanations here cli.vuejs.org/guide/build-targets....

 

Many thanks! I finally figured out how to apply slots 🤓

 

You're most welcome :)