Complete the code to define an error boundary using onErrorCaptured in Vue.
<script setup> import { [1], onErrorCaptured } from 'vue' const error = ref(null) onErrorCaptured((err) => { error.value = err.message return false }) </script>
onErrorCaptured instead of ref to create reactive state.reactive when only a single value is needed.We use ref to create a reactive variable error that stores the error message.
Complete the template to show the error message if an error is captured.
<template> <div v-if="[1]"> <p>Error: {{ error }}</p> </div> <slot v-else /> </template>
error.value inside the template, which is not needed.error.message which is not defined.In the template, refs are accessed directly without .value.
Fix the error in the onErrorCaptured callback to properly stop error propagation.
<script setup> import { ref, onErrorCaptured } from 'vue' const error = ref(null) onErrorCaptured((err) => { error.value = err.message return [1] }) </script>
true which allows the error to propagate.Returning false stops the error from propagating further in Vue's error handling.
Fill both blanks to create a reactive error state and reset it on a button click.
<script setup> import { [1], onErrorCaptured } from 'vue' const error = [2](null) function resetError() { error.value = null } onErrorCaptured((err) => { error.value = err.message return false }) </script>
reactive instead of ref for a single value.Use ref to create a reactive variable and import it from Vue.
Fill all three blanks to create an error boundary component that captures errors, shows a message, and allows reset.
<template> <div v-if="[1]"> <p>Error: {{ error }}</p> <button @click="[2]">Reset</button> </div> <slot v-else /> </template> <script setup> import { ref, onErrorCaptured } from 'vue' const error = ref(null) function [3]() { error.value = null } onErrorCaptured((err) => { error.value = err.message return false }) </script>
The template checks error, the button calls resetError, and the function resetError clears the error.