by Sai gowtham

How to use slots in Vue.js

How to use slots in Vue.js

In this tutorial, we will learn about how to use slots in vue.js with the help of examples.

What are Slots?

Slots help you to pass data between the opening and closing component tags.

In Vue.js props are used to pass the data to its child components but it is hard to pass the complex code. In such cases slots can be used.

Let’s create a new component called Post by adding the <slot> element.

Post.vue
<template>
 <div>
   <slot></slot> </div>
</template>

<script>
export default{

}
</script>

Now, if we pass any content between the Post component opening and closing tags that are rendered in the place of <slot></slot> element.

App.vue
<template>
  <div>
    <Post>
      <h1>What is Lorem Ipsum?</h1>      <p>        What is Lorem Ipsum?Lorem Ipsum is simply dummy text of the printing        andtypesetting industry.Lorem Ipsum has been the industry's standard        dummy.      </p>    </Post>
  </div>
</template>

<script>
import Post from "./components/Post";
export default {
  components: {
    Post
  }
};
</script>

Output:

vue-slots-example

Named Slots

Sometimes, we need to pass data to particular places in such cases named slots can be used.

The named slots can be created by adding a name attribute to the <slot> element.

Post.vue
<template>
 <div>
   <slot name="title"></slot>   <slot name="description"></slot> </div>
</template>

<script>
export default{

}
</script>

To pass the content to the named slots we need to use v-slot directive on template providing slot name as v-slot argument.

App.vue
<template>
  <div>
    <Post>
      <template v-slot:name>         <h1>What is Lorem Ipsum?</h1>
      </template>
      <template v-slot:description>        <p>
          What is Lorem Ipsum?Lorem Ipsum is simply dummy text of the printing
          andtypesetting industry.Lorem Ipsum has been the industry's standard
          dummy.
       </p>
     </template>
    </Post>
  </div>
</template>

<script>
import Post from "./components/Post";
export default {
  components: {
    Post
  }
};
</script>

Fallback data

In some cases, we can use fallback data (aka default) when data is not passed to a slot.

For example:

my-button.vue
<template>
   <button>
       <slot>Submit</slot>   </button>
</template>

In the above component, we have added a Submit text inside a slot element.

Now, If we use a my-button component without passing any data we can see the fallback data Submit text is rendered inside the button.

<template>
  <div>
     <my-button></my-button>  </div>
</template>

<script>
import Mybutton from "./components/my-button.vue";
export default {
  components: {
    "my-button": Mybutton
  }
};
</script>

Output of rendered html:

<div>
   <button>Submit</button></div>

But, if we pass data to the my-button component fallback data is replaced.

<template>
  <div>
     <my-button>Add</my-button>  </div>
</template>

<script>
import Mybutton from "./components/my-button.vue";
export default {
  components: {
    "my-button": Mybutton
  }
};
</script>

Output of rendered html:

<div>
   <button>Add</button></div>

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
221,520 students enrolled
44 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
114,575 students enrolled
21 hours of video content
View Course