How I'm using Vue Slots on my site
Tags: vue, codenewbie, beginners, webdev
Published: 2020-07-08 15:52:08

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eh7s5e7g0hntij8ud0af.png)

Say hi to the first Vue tidbit 👋 It’s about time I start covering Vue in my code tidbits, right 😉‬

Use the new named slot shorthand with "#". This is available now in Vue 2.6.0+ 👍

```vue
<!-- Old -->
<template v-slot:content>

<!-- New -->
<template #content>
```

Vue, hands down, have the BEST documentation EVER! So I'm not going to try to compete with that. Just like how I'd never have the audacity to compete with Serena Williams to a tennis match. Even though I have a pretty mean serve 🎾 (just kidding, I can barley hit the ball 😂).

Instead, I'm going to talk about how I use slots on my site, samanthaming.com 🙋🏻‍♀️

_Note_: This article does assume some basic knowledge of Vue. So if you're a complete newbie to Vue, I'd suggest you check out the Vue docs first:

> [Vue Docs: Introduction](https://vuejs.org/v2/guide/index.html) <br>
> [Vue Docs: Components Basics](https://vuejs.org/v2/guide/components.html)

## What are slots

I like to think of slots as templates. Think of how you create your resume, you don't typically start from an empty document. You will open up Google Docs and find a resume template and build from that. And that's exactly what slots are. It's a template that allow you quickly to fill in the blanks without having to start from scratch. Super efficient 👏

## Components vs Slots explained in Non-Dev Terms

When I first started learning slots, I was super confused. I kept thinking slot was some separate thing. But slot is not. It is a Vue component that has an additional slot functionality to it. It's a component with super power. It's a component that's organized.

Hmmm...I don't think I'm getting anywhere with my explanation and you're probably more confused then you were before 😂 Let's explain it in non-dev terms.

Think of a component as your kitchen drawer. It's an open space storage. But the problem with open space, it can get messy really quick:

<img src="https://cdn.homedit.com/wp-content/uploads/2015/11/Custom-wood-kitchen-drawer-organization-before.jpg" style="height:300px">

<br>

A great way to organize your tools is to use dividers which allows to sort your tools into separate section. And that's exactly what slots are. It helps you organize your content into nice sections 👍

<img src="https://cdn.homedit.com/wp-content/uploads/2015/11/Custom-wood-kitchen-drawer-organization-after.jpg" style="height:300px">

_Image Credit: https://www.homedit.com/drawer-organizing-tips/_

Isn't it much better! Super [Marie Kondo](https://konmari.com/) if you ask me ✨

## How My Site Is Using Slots

My entire site is built on slots. The prime example is my article pages. These are what I call article pages:

```bash
/tidbits/some-code-note-article
# ex. https://www.samanthaming.com/tidbits/82-html-audio-tag/

/blog/some-blog-article
# ex. https://www.samanthaming.com/blog/how-to-ace-the-developer-interview/

/flexbox30/some-flexbox-article
# ex. https://www.samanthaming.com/flexbox30/1-flexbox-intro/
```

If you visit those sites, you will notice they all look quite similar. That's because they're all using a slot. So let's go through step-by-step on how I build this.

Note: I'm going to simplify it a bit that way it will be easier for you to follow. Alright let's go! 💪

### 1. The Layout

When you build the slot, it's a good idea to plan out your layout. And this what my article layout looks like.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f0obrcozhuhaw7vbwma6.jpeg)

So in my layout, I have 5 slots:

- `article-header`
- `article-content`
- `article-footer`
- `side`
- `banner`

### 2. Build the slot

Building a slot is no different than building a component. Essentially, slot is a component with super power. Here's how the component looks like:

```vue
<!-- ArticleLayout.vue -->
<template>
  <div>
    <article>
      <slot name="articleHeader" />
      <slot name="articleContent" />
      <slot name="articleFooter" />
    </article>
    <aside>
      <slot name="side" />
    <aside>
    <div>
      <slot name="banner" />
    </div>
  </div>
</template>
```

### 3. Consuming the slot

Alright, we made our slots. Next, let's stick some stuff in it.

```vue
<!-- TidbitPage.vue -->
<article-layout>

  <template #articleHeader>
    <h1>I am the header</h1>
  </template>

</article-layout>
```

So let's breakdown what we're doing here. The first thing we're doing is calling our `article-layout` component. Then I'm inserting content to my slot by wrapping it in a `<template>` tag and referencing the slot name with `#`. And inside the `<template>` is where I insert my content.

### 4. Final

Putting it altogether, it'd look something like this:

```vue
<!-- TidbitPage.vue -->
<template>
  <article-layout>

    <template #articleHeader>
      <h1>I am the header</h1>
    </template>

    <template #articleContent>
      <p>I am the content</p>
    </template>

    <template #articleFooter>
      <footer>I am the footer</footer>
    </template>

    <template #side>
      <aside>I am the side stuff</aside>
    </template>

    <template #banner>
      <div>I am the banner</div>
    </template>

  </article-layout>
<template>
```

## Resources

- [Vue Docs: Named Slots Shorthand](https://vuejs.org/v2/guide/components-slots.html#Named-Slots-Shorthand)
- [Vue Docs: Components Basics](https://vuejs.org/v2/guide/components.html)
- [CSS Tricks: Intro to Vue.js - Components, Props, and Slots](https://css-tricks.com/intro-to-vue-2-components-props-slots/)
- [Scott Stern: Vue Scoped Slots - WTF are they and how do I use them?](https://dev.to/scottstern06/vue-scoped-slots-wtf-are-they-and-how-do-i-use-them-59a3)

---

**Thanks for reading ❤**
To find more code tidbits, please visit [samanthaming.com](https://www.samanthaming.com/)

<table>
  <tbody>
    <tr>
      <td>
        🎨<a href="https://www.instagram.com/samanthaming/">Instagram</a>
      </td>
      <td>
        🌟<a href="https://twitter.com/samantha_ming">Twitter</a>
      </td>
      <td>
        👩🏻‍💻<a href="https://www.instagram.com/samanthaming/">SamanthaMing.com</a>
      </td>
    </tr>
  </tbody>
</table>