Skip to content

Commit 480c95a

Browse files
noookskirtles-code
andauthored
docs: document mixing named and default slots (#1946)
* docs: document mixing named and default slots vuejs/core#6549 * chore: update component example, and section * docs: move section to named scoped slots * Update src/guide/components/slots.md Co-authored-by: skirtle <[email protected]> * Update src/guide/components/slots.md Co-authored-by: skirtle <[email protected]> * Update src/guide/components/slots.md Co-authored-by: skirtle <[email protected]> * Update src/guide/components/slots.md Co-authored-by: skirtle <[email protected]> Co-authored-by: skirtle <[email protected]>
1 parent f25e283 commit 480c95a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/guide/components/slots.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,38 @@ Passing props to a named slot:
412412

413413
Note the `name` of a slot won't be included in the props because it is reserved - so the resulting `headerProps` would be `{ message: 'hello' }`.
414414

415+
If you are mixing named slots with the default scoped slot, you need to use an explicit `<template>` tag for the default slot. Attempting to place the `v-slot` directive directly on the component will result in a compilation error. This is to avoid any ambiguity about the scope of the props of the default slot. For example:
416+
417+
```vue-html
418+
<!-- This template won't compile -->
419+
<template>
420+
<MyComponent v-slot="{ message }">
421+
<p>{{ message }}</p>
422+
<template #footer>
423+
<!-- message belongs to the default slot, and is not available here -->
424+
<p>{{ message }}</p>
425+
</template>
426+
</MyComponent>
427+
</template>
428+
```
429+
430+
Using an explicit `<template>` tag for the default slot helps to make it clear that the `message` prop is not available inside the other slot:
431+
432+
```vue-html
433+
<template>
434+
<MyComponent>
435+
<!-- Use explicit default slot -->
436+
<template #default="{ message }">
437+
<p>{{ message }}</p>
438+
</template>
439+
440+
<template #footer>
441+
<p>Here's some contact info</p>
442+
</template>
443+
</MyComponent>
444+
</template>
445+
```
446+
415447
### Fancy List Example
416448

417449
You may be wondering what would be a good use case for scoped slots. Here's an example: imagine a `<FancyList>` component that renders a list of items - it may encapsulate the logic for loading remote data, using the data to display a list, or even advanced features like pagination or infinite scrolling. However, we want it to be flexible with how each item looks and leave the styling of each item to the parent component consuming it. So the desired usage may look like this:

0 commit comments

Comments
 (0)