0
0
Vueframework~20 mins

Named slots and scoped slots in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Slots Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Vue component using named slots?

Consider the following Vue component usage with named slots. What will be the rendered HTML inside the <Card> component?

Vue
<template>
  <Card>
    <template #header>
      <h1>Title</h1>
    </template>
    <template #footer>
      <p>Footer text</p>
    </template>
    <p>Main content</p>
  </Card>
</template>

<script setup>
import Card from './Card.vue'
</script>
A<div class="card"><p>Footer text</p><h1>Title</h1><p>Main content</p></div>
B<div class="card"><h1>Title</h1><p>Footer text</p></div>
C<div class="card"><p>Main content</p></div>
D<div class="card"><h1>Title</h1><p>Main content</p><p>Footer text</p></div>
Attempts:
2 left
💡 Hint

Remember that named slots are placed where their slot names are declared inside the component template.

state_output
intermediate
2:00remaining
What is the value of itemText inside the scoped slot?

Given this parent and child component using a scoped slot, what will be the value of itemText inside the slot?

Vue
<!-- Parent.vue -->
<template>
  <List :items="['apple', 'banana']">
    <template #default="{ item }">
      <p>{{ itemText }}</p>
    </template>
  </List>
</template>

<script setup>
import List from './List.vue'
const itemText = 'fruit'
</script>

<!-- List.vue -->
<template>
  <ul>
    <li v-for="item in items" :key="item">
      <slot :item="item" />
    </li>
  </ul>
</template>

<script setup>
const props = defineProps({ items: Array })
</script>
Afruit
Bapple
Cbanana
Dundefined
Attempts:
2 left
💡 Hint

Scoped slot templates have access to both the slot props and the parent component's scope.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a scoped slot in Vue 3?

Which of the following slot usage syntaxes correctly passes a scoped slot prop named user?

A<template #default="{ user }">{{ user.name }}</template>
B<template v-slot:default="user">{{ user.name }}</template>
C<template #default="user">{{ user.name }}</template>
D<template v-slot:user="{ user }">{{ user.name }}</template>
Attempts:
2 left
💡 Hint

Remember the correct syntax for destructuring slot props in Vue 3.

🔧 Debug
advanced
2:00remaining
Why does this scoped slot not display the passed data?

Given this code, why does the slot not show the message?

Vue
<!-- Parent.vue -->
<template>
  <Child>
    <template #default="props">
      <p>{{ message }}</p>
    </template>
  </Child>
</template>

<script setup>
import Child from './Child.vue'
</script>

<!-- Child.vue -->
<template>
  <slot :message="'Hello from child'" />
</template>
AThe child component does not pass any props to the slot
BThe slot is missing a name attribute
C<code>message</code> is not defined in the slot scope; should use <code>props.message</code>
DThe parent component must use <code>v-slot</code> instead of <code>#default</code>
Attempts:
2 left
💡 Hint

Check how slot props are accessed inside the slot template.

🧠 Conceptual
expert
2:00remaining
Which statement about named and scoped slots in Vue 3 is TRUE?

Choose the correct statement about how named slots and scoped slots work in Vue 3.

ANamed slots can pass data to the parent via slot props, but default slots cannot.
BScoped slots allow the child component to send data to the parent through slot props regardless of slot name.
COnly the default slot can be scoped; named slots cannot have slot props.
DSlot props are only available in the child component, not in the parent slot template.
Attempts:
2 left
💡 Hint

Think about how slot props work with named and default slots.