-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
dtechmaster
wants to merge
1
commit into
vuejs:main
Choose a base branch
from
dtechmaster:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
|
||
## 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> | ||
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']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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