Slots let you put content inside a component from outside. The default slot is the main place where you add this content.
Slot basics (default slot) in Svelte
<slot></slot>
The <slot> tag marks where the outside content will appear.
If no content is given, the slot can show default content inside <slot> like <slot>Default text</slot>.
<slot></slot>
<slot>Default content here</slot>
<div class="wrapper"> <slot></slot> </div>
The Box component uses a default slot with fallback text. In App.svelte, the first <Box> has custom content, so it shows that. The second <Box> has no content, so it shows the default fallback text.
<!-- Box.svelte --> <div class="box"> <slot>Default box content</slot> </div> <style> .box { padding: 1rem; border: 2px solid #007acc; border-radius: 0.5rem; background-color: #e0f0ff; } </style> <!-- App.svelte --> <script> import Box from './Box.svelte'; </script> <Box> <p>This is custom content inside the box.</p> </Box> <Box></Box>
Slots help keep components flexible and reusable by letting you insert any content.
Default slot content only shows if no content is passed inside the component.
You can style the slot container to control how inserted content looks.
Slots let you pass content into components from outside.
The default slot is the main place where this content goes.
If no content is given, default slot content shows instead.
Practice
Solution
Step 1: Understand what a slot does
A slot allows a parent component to pass content into a child component.Step 2: Identify the role of the default slot
The default slot is the unnamed slot that receives content if no specific slot name is given.Final Answer:
To display content passed from the parent component if no named slot is used -> Option AQuick Check:
Default slot = main content placeholder [OK]
- Confusing slots with CSS or state
- Thinking default slot is for styles
- Assuming default slot imports scripts
Solution
Step 1: Recall slot syntax in Svelte
The default slot is defined simply with <slot></slot> without a name attribute.Step 2: Check the options for correct syntax
Only uses <slot></slot> without a name, which is correct for the default slot.Final Answer:
<slot></slot> -> Option AQuick Check:
Default slot = <slot></slot> [OK]
- Adding name="default" attribute incorrectly
- Using custom tags like
- Confusing named slots with default slot syntax
<!-- Child.svelte --> <slot>No content provided</slot> <!-- Parent.svelte --> <Child> <p>Hello from parent</p> </Child>
Solution
Step 1: Understand default slot behavior
The <slot> tag shows content passed from the parent if any is provided.Step 2: Check if parent passes content
The parent passes <p>Hello from parent</p> inside <Child>, so this content replaces the default slot fallback.Final Answer:
Hello from parent -> Option BQuick Check:
Parent content replaces default slot fallback [OK]
- Assuming fallback text always shows
- Thinking slot content is ignored
- Expecting an error when slot is used
<slot name>Fallback</slot>
Solution
Step 1: Analyze the slot tag attributes
The slot tag has a name attribute but no value, which is invalid syntax.Step 2: Recall correct slot syntax
Named slots require a name with a string value, or omit the name attribute for default slot.Final Answer:
The slot tag should not have a name attribute without a value -> Option CQuick Check:
Empty name attribute on slot is invalid [OK]
- Leaving name attribute empty
- Thinking fallback content is invalid
- Using self-closing slot tags incorrectly
Solution
Step 1: Understand default slot fallback usage
The default slot shows fallback content if no content is passed from the parent.Step 2: Identify correct fallback syntax
Using <slot>Welcome!</slot> sets "Welcome!" as fallback text correctly.Step 3: Check other options for errors
<slot name="default">Welcome!</slot> incorrectly uses name="default" which is unnecessary; C uses invalid expression inside slot; D uses non-existent fallback attribute.Final Answer:
<slot>Welcome!</slot> -> Option DQuick Check:
Default slot fallback = content inside <slot> [OK]
- Adding name="default" attribute unnecessarily
- Trying to use JavaScript expressions inside slot tags
- Using invalid fallback attributes
Ready to prove it?
Pass the test and this pattern moves unknown
