Complete the code to define a parent component that provides context to its children.
<script setup> import { ref, provide } from 'vue' const state = ref(false) provide('[1]', state) </script>
The parent component uses provide to share toggleState with its children.
Complete the code to inject the provided state in a child component.
<script setup> import { inject } from 'vue' const toggleState = inject('[1]') </script>
The child component uses inject with the same key toggleState to access the parent's state.
Fix the error in the child component to toggle the shared state correctly.
<script setup> import { inject } from 'vue' const toggleState = inject('toggleState') function toggle() { toggleState[1] = !toggleState.value } </script>
Since toggleState is a ref, we assign to toggleState.value. The code should be toggleState.value = !toggleState.value.
Fill both blanks to create a compound component with a parent and a child that toggles visibility.
<template>
<div>
<button @click="[1]">Toggle</button>
<slot v-if="[2]"></slot>
</div>
</template>
<script setup>
import { ref } from 'vue'
const visible = ref(false)
function toggle() {
visible.value = !visible.value
}
</script>The button calls the toggle function on click, and the slot is shown only if visible.value is true.
Fill all three blanks to complete a compound component pattern with provide/inject and toggle logic.
<script setup> import { ref, provide, inject } from 'vue' const visible = ref(false) function toggle() { visible.value = !visible.value } provide('[1]', visible) provide('[2]', toggle) const injectedVisible = inject('[3]') </script>
The parent provides visible and toggle. The child injects visible to read the state.