SoFunction
Updated on 2025-04-13

Detailed explanation of the use of slots for front-end Vue2, Vue3 and different versions of nuxt

Slots in Vue2

Basic slot

In Vue2, the base slot allows a placeholder to be defined in the component's template and then custom content is inserted when using the component. For example, create a simpleMyBoxComponents:

<template>
  <div class="box">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: 'MyBox'
}
</script>

<style scoped>
.box {
  border: 1px solid #ccc;
  padding: 10px;
}
</style>

When using:

&lt;template&gt;
  &lt;div&gt;
    &lt;MyBox&gt;
      &lt;p&gt;This is inserted intoMyBoxContents in component slots&lt;/p&gt;
    &lt;/MyBox&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import MyBox from './';
export default {
  components: {
    MyBox
  }
}
&lt;/script&gt;

The base slot itself does not involve data transfer and is mainly used for simple content insertion.

Named slots

Named slots allow multiple slots to be defined in a component and distinguished by name. For example:

<template>
  <div class="layout">
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>

<script>
export default {
  name: 'MyLayout'
}
</script>

<style scoped>
.layout {
  display: flex;
  flex-direction: column;
}
</style>

When using:

&lt;template&gt;
  &lt;div&gt;
    &lt;MyLayout&gt;
      &lt;template v-slot:header&gt;
        &lt;h1&gt;Page title&lt;/h1&gt;
      &lt;/template&gt;
      &lt;p&gt;Page main content&lt;/p&gt;
      &lt;template v-slot:footer&gt;
        &lt;p&gt;all rights reserved &amp;copy; 2024&lt;/p&gt;
      &lt;/template&gt;
    &lt;/MyLayout&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import MyLayout from './';
export default {
  components: {
    MyLayout
  }
}
&lt;/script&gt;

Named slots are also mainly used for content organization, and usually do not directly pass data. However, data-related display can be indirectly implemented in conjunction with other Vue features, such as props.

Scope slots - Data delivery

Scope slots allow subcomponents to pass data to the slots. For example, there is a user listUserListComponents need to display the user's basic information and custom operations in the list item:

&lt;template&gt;
  &lt;ul&gt;
    &lt;li v-for="user in users" :key=""&gt;
      &lt;slot :user="user"&gt;
        &lt;span&gt;{{  }} - {{  }}&lt;/span&gt;
      &lt;/slot&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data() {
    return {
      users: [
        { id: 1, name: 'Zhang San', age: 25 },
        { id: 2, name: 'Li Si', age: 30 }
      ]
    };
  }
}
&lt;/script&gt;

When using:

&lt;template&gt;
  &lt;div&gt;
    &lt;UserList&gt;
      &lt;template v-slot:default="slotProps"&gt;
        &lt;span&gt;{{  }} - {{  }}&lt;/span&gt;
        &lt;button @click="handleClick()"&gt;operate&lt;/button&gt;
      &lt;/template&gt;
    &lt;/UserList&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import UserList from './';
export default {
  components: {
    UserList
  },
  methods: {
    handleClick(user) {
      (`To the user${}进行operate`);
    }
  }
}
&lt;/script&gt;

Here subcomponentUserListpassslotProperties of tags:user="user"Pass user data to slots, when the parent component uses slots,v-slot:default="slotProps"Receive data,slotPropsis an object that contains the child components passeduserdata.

Slots in Vue3

Vue3 basically continues the syntax of Vue2 in the use of slots, but has improved in some details.

Scope slots - Data delivery

In Vue3, usev-slotThe instructions are more concise. For example, create aMyListComponent display product list:

&lt;template&gt;
  &lt;ul&gt;
    &lt;li v-for="item in items" :key=""&gt;
      &lt;slot :item="item"&gt;{{  }}&lt;/slot&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'apple', price: 5 },
        { id: 2, name: 'banana', price: 3 }
      ]
    };
  }
}
&lt;/script&gt;

When using:

&lt;template&gt;
  &lt;div&gt;
    &lt;MyList&gt;
      &lt;template v-slot="{ item }"&gt;
        &lt;span&gt;{{  }} - price: {{  }}&lt;/span&gt;
      &lt;/template&gt;
    &lt;/MyList&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
import MyList from './';
export default {
  components: {
    MyList
  }
}
&lt;/script&gt;

herev-slotThe object is deconstructed later, and the data passed by the child component is directly obtained.item, more concise and intuitive than Vue2. At the same time, in Vue3, it can also be usedv-slotWith dynamic parameters, more flexible data transfer and slot usage are achieved.

Slots in Nuxt

Nuxt2

In Nuxt2, slots are widely used in page layouts and components. For example, in the defaultlayouts/middle:

<template>
  <div>
    <nuxt-head></nuxt-head>
    <header>
      <slot name="header"></slot>
    </header>
    <main>
      <nuxt></nuxt>
    </main>
    <footer>
      <slot name="footer"></slot>
    </footer>
  </div>
</template>

When used in the page:

&lt;template&gt;
  &lt;div&gt;
    &lt;template v-slot:header&gt;
      &lt;h1&gt;My page title&lt;/h1&gt;
    &lt;/template&gt;
    &lt;p&gt;Page content&lt;/p&gt;
    &lt;template v-slot:footer&gt;
      &lt;p&gt;Information at the bottom of the page&lt;/p&gt;
    &lt;/template&gt;
  &lt;/div&gt;
&lt;/template&gt;

For data transfer, Nuxt2 can be passed in the page componentthis.$root.$dataAccess global data in other ways, and then display it in the slot content. For example,layouts/Define a global website name data inheaderDisplayed in slots:

&lt;template&gt;
  &lt;div&gt;
    &lt;nuxt-head&gt;&lt;/nuxt-head&gt;
    &lt;header&gt;
      &lt;slot name="header"&gt;{{ $root.$ }}&lt;/slot&gt;
    &lt;/header&gt;
    &lt;main&gt;
      &lt;nuxt&gt;&lt;/nuxt&gt;
    &lt;/main&gt;
    &lt;footer&gt;
      &lt;slot name="footer"&gt;&lt;/slot&gt;
    &lt;/footer&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data() {
    return {
      siteName: 'My website'
    };
  }
}
&lt;/script&gt;

When used in the page:

&lt;template&gt;
  &lt;div&gt;
    &lt;template v-slot:header&gt;
      &lt;h1&gt;{{ $root.$ }} - Page title&lt;/h1&gt;
    &lt;/template&gt;
    &lt;p&gt;Page content&lt;/p&gt;
    &lt;template v-slot:footer&gt;
      &lt;p&gt;Information at the bottom of the page&lt;/p&gt;
    &lt;/template&gt;
  &lt;/div&gt;
&lt;/template&gt;

Nuxt3

Nuxt3 is based on Vue3, and the use of slots is simpler and more powerful. For example,layouts/middle:

<template>
  <div>
    <Head />
    <header>
      <slot name="header" />
    </header>
    <main>
      <slot />
    </main>
    <footer>
      <slot name="footer" />
    </footer>
  </div>
</template>

Use in the page:

&lt;template&gt;
  &lt;div&gt;
    &lt;template #header&gt;
      &lt;h1&gt;Nuxt3Page title&lt;/h1&gt;
    &lt;/template&gt;
    &lt;p&gt;Nuxt3Page content&lt;/p&gt;
    &lt;template #footer&gt;
      &lt;p&gt;Nuxt3At the bottom of the page&lt;/p&gt;
    &lt;/template&gt;
  &lt;/div&gt;
&lt;/template&gt;

In terms of data transfer, Nuxt3 can utilize the combination APIuseStateWait for functions to share data. For example, create a shared user login status data,headerDifferent contents are displayed in the slot according to the login status:

&lt;template&gt;
  &lt;div&gt;
    &lt;Head /&gt;
    &lt;header&gt;
      &lt;slot name="header"&gt;
        &lt;template v-if="isLoggedIn"&gt;
          &lt;span&gt;welcome,{{  }}&lt;/span&gt;
        &lt;/template&gt;
        &lt;template v-else&gt;
          &lt;a href="/login" rel="external nofollow" &gt;Log in&lt;/a&gt;
        &lt;/template&gt;
      &lt;/slot&gt;
    &lt;/header&gt;
    &lt;main&gt;
      &lt;slot /&gt;
    &lt;/main&gt;
    &lt;footer&gt;
      &lt;slot name="footer" /&gt;
    &lt;/footer&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script setup&gt;
import { useState } from '#imports';
const { state: user, isLoggedIn } = useState('user', () =&gt; ({
  name: '',
  loggedIn: false
}));
&lt;/script&gt;

When used in the page:

&lt;template&gt;
  &lt;div&gt;
    &lt;template #header&gt;
      &lt;!-- Here you can override the default according to your needsheaderSlot content --&gt;
    &lt;/template&gt;
    &lt;p&gt;Nuxt3Page content&lt;/p&gt;
    &lt;template #footer&gt;
      &lt;p&gt;Nuxt3At the bottom of the page&lt;/p&gt;
    &lt;/template&gt;
  &lt;/div&gt;
&lt;/template&gt;

Summarize

Slots are very important features in Vue2, Vue3 and different versions of Nuxt. They can not only enable flexible insertion of content, but also enable data transmission through scoped slots and other methods. By rationally using slots and their data transfer functions, components can be reusable and flexible, and more complex and efficient applications can be built. Whether it is a basic slot, a named slot or a scope slot, it plays a key role in different scenarios. Developers need to choose appropriate slot usage methods and data delivery strategies based on specific needs.

This is the article about the use of slots in front-end Vue2, Vue3 and different versions of nuxt. For more related content on vue and nuxt slots, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!