Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to improve Dynamic Slot Names documentation #3038

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 81 additions & 12 deletions src/guide/components/slots.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,22 +327,91 @@ When the header / footer / default is present we want to wrap them to provide ad

## Dynamic Slot Names {#dynamic-slot-names}

[Dynamic directive arguments](/guide/essentials/template-syntax.md#dynamic-arguments) also work on `v-slot`, allowing the definition of dynamic slot names:
Dynamic slot names in Vue allow you to create flexible components by generating slot names at runtime. This is particularly useful when your component's content structure depends on dynamic data.

```vue-html
<base-layout>
<template v-slot:[dynamicSlotName]>
...
</template>
### Declaring Dynamic Slots in the Child Component

<!-- with shorthand -->
<template #[dynamicSlotName]>
...
</template>
</base-layout>
You can declare multiple dynamic slots by iterating over your data. For example, if you have an array of slot names, you can use the `v-for` directive to create a `<slot>` for each name in the array:

```vue
<!-- ChildComponent.vue -->
<template>
<div>
<!-- Loop over slotNames to create dynamic slots -->
<div v-for="name in slotNames" :key="name">
<slot :name="name">
<!-- Fallback content -->
Default content for {{ name }}
</slot>
</div>
</div>
</template>

<script setup>
const props = defineProps({
slotNames: {
type: Array,
required: true,
},
});
</script>
```

Do note the expression is subject to the [syntax constraints](/guide/essentials/template-syntax.md#dynamic-argument-syntax-constraints) of dynamic directive arguments.
In this example:

- **`slotNames` Prop**: An array of strings representing the names of the slots.
- **Dynamic Slots**: The component uses `v-for` to create a `<slot>` for each name in `slotNames`.

### Using Dynamic Slots in the Parent Component

In the parent component, provide content for these dynamic slots using the `v-slot` directive with the slot names in a static or dynamic way:


```vue
<!-- ParentComponent.vue (Static way) -->
<template>
<ChildComponent :slotNames="slotNames">
<!-- Provide content for 'header' slot -->
<template #header>
<h1>Header Content</h1>
</template>

<!-- Provide content for 'footer' slot -->
<template #footer>
<p>Footer Content</p>
</template>
</ChildComponent>
</template>

<script setup>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: we need to wrap this in <div class="composition-api"></div> and write the matching code for Options API

import ChildComponent from './ChildComponent.vue';

const slotNames = ['header', 'footer'];
</script>
```
OR

```vue
<!-- ParentComponent.vue (Dynamic way) -->
<template>
<ChildComponent :slotNames="slotNames">
<template v-for="name in slotNames" #[name]>
<h1>{{ name }} Content</h1>
</template>
</ChildComponent>
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';

const slotNames = ['header', 'footer'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same note as above about the Options API

</script>
```

Here:

- **Passing `slotNames`**: The parent passes an array `['header', 'footer']` to the child component.
- **Providing Slot Content**: Uses the shorthand `#slotName` to provide content for each dynamic slot.

## Scoped Slots {#scoped-slots}

Expand Down