Complete the code to provide a value using Vue's provide function.
<script setup> import { provide } from 'vue' const message = 'Hello from provider' provide([1], message) </script>
The provide function needs a key as the first argument to identify the value. Here, 'messageKey' is the key used to provide the message.
Complete the code to inject the provided value using Vue's inject function.
<script setup> import { inject } from 'vue' const receivedMessage = inject([1]) </script>
The inject function requires the same key used in provide to receive the value. Here, 'messageKey' is the key.
Fix the error in the code to correctly inject a default value if the key is not provided.
<script setup> import { inject } from 'vue' const count = inject('countKey', [1]) </script>
The second argument to inject is the default value if the key is not found. Here, 0 is a number default count.
Fill both blanks to provide and inject a reactive value using Vue's ref.
<script setup> import { ref, provide, inject } from 'vue' const count = ref(0) provide([1], count) const injectedCount = inject([2]) </script>
Both provide and inject must use the same key string to share the reactive count value. Here, 'countKey' is used for both.
Fill all three blanks to create a reactive object, provide it, and inject it in a child component.
<script setup> import { reactive, provide, inject } from 'vue' const state = reactive({ count: 0 }) provide([1], state) const injectedState = inject([2]) console.log(injectedState[3]) </script>
The reactive object state is provided and injected using the same key 'stateKey'. Accessing injectedState.count shows the reactive count value.