You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/components/slots.md
+81-12
Original file line number
Diff line number
Diff line change
@@ -327,22 +327,91 @@ When the header / footer / default is present we want to wrap them to provide ad
327
327
328
328
## Dynamic Slot Names {#dynamic-slot-names}
329
329
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.
331
331
332
-
```vue-html
333
-
<base-layout>
334
-
<template v-slot:[dynamicSlotName]>
335
-
...
336
-
</template>
332
+
### Declaring Dynamic Slots in the Child Component
337
333
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>
343
358
```
344
359
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.
0 commit comments