Consider the following Vue component usage with named slots. What will be the rendered HTML inside the <Card> component?
<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>Remember that named slots are placed where their slot names are declared inside the component template.
The Card component places the header slot content first, then the default slot content, then the footer slot content. So the output includes the header <h1>Title</h1>, the main content <p>Main content</p>, and the footer <p>Footer text</p> in that order.
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?
<!-- 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>
Scoped slot templates have access to both the slot props and the parent component's scope.
The itemText variable is declared in the parent component's <script setup> and is accessible inside the scoped slot template because slot content has access to the parent scope in addition to the slot props provided by the child.
Which of the following slot usage syntaxes correctly passes a scoped slot prop named user?
Remember the correct syntax for destructuring slot props in Vue 3.
Option A correctly uses destructuring in the slot scope to access user. Option A passes user as an object but does not destructure it, so user.name would be undefined. Option A uses shorthand but does not destructure, so user.name is undefined. Option A uses a wrong slot name user instead of default.
Given this code, why does the slot not show the message?
<!-- 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>Check how slot props are accessed inside the slot template.
The slot scope object is named props. The variable message is inside props, so you must use {{ props.message }} to access it. Using {{ message }} alone looks for a variable in the parent scope, which does not exist.
Choose the correct statement about how named slots and scoped slots work in Vue 3.
Think about how slot props work with named and default slots.
Scoped slots allow the child component to pass data to the parent through slot props for any slot, named or default. So option B is true. Option B is false because default slots can also be scoped. Option B is false because named slots can have slot props. Option B is false because slot props are accessed in the parent slot template.