Vue3 Api Slot
π
2026-06-23 | π Vue.js
# Vue3 Slots (`
`)
In Vue 3, slots are a powerful mechanism that allows parent components to pass template content down to child components. You can think of a slot as a placeholder or a "portal" inside the child component. The parent component can inject any HTML elements, text, or other Vue components into these placeholders.
This architecture enables child components to remain highly reusable and flexible, as they do not need to hardcode their internal layout or content structure.
---
## Basic Usage of Slots
### 1. Default Slots
The default slot is the simplest type of slot. In the child component, you define a placeholder using the `` tag. When the parent component renders the child, any content placed between the child component's opening and closing tags will be injected into that `` placeholder.
#### Child Component (`ChildComponent.vue`)
```vue
This is the child component title
Default fallback content
```
#### Parent Component (`ParentComponent.vue`)
```vue
This is custom content passed from the parent component.
```
#### How it works:
* The content `This is custom content passed from the parent component.
` replaces the `` tag in the child component.
* If the parent component uses `` without passing any inner content, the child will render its fallback content: `"Default fallback content"`.
---
### 2. Named Slots
Named slots allow you to define multiple slot placeholders within a single child component. By assigning a `name` attribute to each ``, you can target specific locations. In the parent component, you target these slots using the `v-slot` directive (or its shorthand `#`).
#### Child Component (`ChildComponent.vue`)
```vue
Default Main Content
```
#### Parent Component (`ParentComponent.vue`)
```vue
Custom Header Content
This is the main body content passed from the parent.
Custom Footer Content
```
#### Key Points:
* Any content not wrapped in a `` block is automatically directed to the default, unnamed slot.
* The `v-slot:slotName` directive can be abbreviated to `#slotName` (e.g., `#header` and `#footer`).
---
## Scoped Slots
Scoped slots allow a child component to pass data back up to the parent component's slot content. This is incredibly useful when the parent needs to customize how the child's internal data is rendered.
### Basic Usage of Scoped Slots
In the child component, you bind data attributes to the `` element using `v-bind` (or the `:` shorthand). In the parent component, you receive these attributes as a slot props object via the `v-slot` directive.
#### Child Component (`ChildComponent.vue`)
```vue
```
#### Parent Component (`ParentComponent.vue`)
```vue
User Name: {{ user.name }}
User Age: {{ user.age }}
```
#### How it works:
1. The child component exposes its local `user` state to the slot via `:user="user"`.
2. The parent component accesses this data using `v-slot="{ user }"`.
3. The parent can now dynamically render the child's internal state using its own custom markup.
---
## Important Considerations
1. **Compilation Scope**:
Everything in the parent template is compiled in the parent scope; everything in the child template is compiled in the child scope. A parent template cannot directly access data properties inside a child component unless you use **Scoped Slots**.
2. **Shorthand Syntax**:
Always prefer the `#` shorthand for named and scoped slots in production code to keep templates clean:
* `v-slot:header` becomes `#header`
* `v-slot="{ user }"` becomes `#default="{ user }"` (when targeting the default slot with scoped props)
3. **Destructuring Slot Props**:
When working with scoped slots, you can destructure the props object directly in the directive expression (e.g., `v-slot="{ user, info }"`), and even assign aliases or default values if needed: `v-slot="{ user: person }"` (renames `user` to `person` inside the parent template).