Skip to content

Commit e7e5e7d

Browse files
authored
Try to improve Dynamic Slot Names documentation
Propose explanation on how to declare Dynamic Slot Names and not only on how to use them.
1 parent 5094e84 commit e7e5e7d

File tree

1 file changed

+81
-12
lines changed

1 file changed

+81
-12
lines changed

Diff for: src/guide/components/slots.md

+81-12
Original file line numberDiff line numberDiff line change
@@ -327,22 +327,91 @@ When the header / footer / default is present we want to wrap them to provide ad
327327

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

330-
[Dynamic directive arguments](/guide/essentials/template-syntax.md#dynamic-arguments) also work on `v-slot`, allowing the definition of dynamic slot names:
330+
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.
331331

332-
```vue-html
333-
<base-layout>
334-
<template v-slot:[dynamicSlotName]>
335-
...
336-
</template>
332+
### Declaring Dynamic Slots in the Child Component
337333

338-
<!-- with shorthand -->
339-
<template #[dynamicSlotName]>
340-
...
341-
</template>
342-
</base-layout>
334+
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:
335+
336+
```vue
337+
<!-- ChildComponent.vue -->
338+
<template>
339+
<div>
340+
<!-- Loop over slotNames to create dynamic slots -->
341+
<div v-for="name in slotNames" :key="name">
342+
<slot :name="name">
343+
<!-- Fallback content -->
344+
Default content for {{ name }}
345+
</slot>
346+
</div>
347+
</div>
348+
</template>
349+
350+
<script setup>
351+
const props = defineProps({
352+
slotNames: {
353+
type: Array,
354+
required: true,
355+
},
356+
});
357+
</script>
343358
```
344359

345-
Do note the expression is subject to the [syntax constraints](/guide/essentials/template-syntax.md#dynamic-argument-syntax-constraints) of dynamic directive arguments.
360+
In this example:
361+
362+
- **`slotNames` Prop**: An array of strings representing the names of the slots.
363+
- **Dynamic Slots**: The component uses `v-for` to create a `<slot>` for each name in `slotNames`.
364+
365+
### Using Dynamic Slots in the Parent Component
366+
367+
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:
368+
369+
370+
```vue
371+
<!-- ParentComponent.vue (Static way) -->
372+
<template>
373+
<ChildComponent :slotNames="slotNames">
374+
<!-- Provide content for 'header' slot -->
375+
<template #header>
376+
<h1>Header Content</h1>
377+
</template>
378+
379+
<!-- Provide content for 'footer' slot -->
380+
<template #footer>
381+
<p>Footer Content</p>
382+
</template>
383+
</ChildComponent>
384+
</template>
385+
386+
<script setup>
387+
import ChildComponent from './ChildComponent.vue';
388+
389+
const slotNames = ['header', 'footer'];
390+
</script>
391+
```
392+
OR
393+
394+
```vue
395+
<!-- ParentComponent.vue (Dynamic way) -->
396+
<template>
397+
<ChildComponent :slotNames="slotNames">
398+
<template v-for="name in slotNames" #[name]>
399+
<h1>{{ name }} Content</h1>
400+
</template>
401+
</ChildComponent>
402+
</template>
403+
404+
<script setup>
405+
import ChildComponent from './ChildComponent.vue';
406+
407+
const slotNames = ['header', 'footer'];
408+
</script>
409+
```
410+
411+
Here:
412+
413+
- **Passing `slotNames`**: The parent passes an array `['header', 'footer']` to the child component.
414+
- **Providing Slot Content**: Uses the shorthand `#slotName` to provide content for each dynamic slot.
346415

347416
## Scoped Slots {#scoped-slots}
348417

0 commit comments

Comments
 (0)