Consider a Vue 3 component using onErrorCaptured to catch errors from its child components. What will be the rendered output if the child throws an error?
<script setup> import { ref, onErrorCaptured } from 'vue' const errorMessage = ref('') onErrorCaptured((err) => { errorMessage.value = 'Error caught: ' + err.message return false }) </script> <template> <div> <ChildComponent /> <p v-if="errorMessage">{{ errorMessage }}</p> </div> </template>
Think about what onErrorCaptured does and how returning false affects error propagation.
Returning false in onErrorCaptured stops the error from propagating further. The error message is set in the reactive errorMessage and displayed in the template.
Choose the correct syntax for onErrorCaptured that catches an error and stops it from propagating further.
Remember that returning false stops propagation.
Returning false inside onErrorCaptured prevents the error from propagating to parent components. Option D correctly returns false.
Given this code snippet, why does the error thrown in ChildComponent not get caught by onErrorCaptured in the parent?
<script setup> import { onErrorCaptured } from 'vue' onErrorCaptured((err) => { console.log('Error caught:', err.message) return false }) </script> <template> <ChildComponent /> </template> <script> export default { components: { ChildComponent: { setup() { throw new Error('Oops!') }, template: '<div>Child</div>' } } } </script>
Consider the lifecycle order of setup and error capturing.
Errors thrown during the setup phase of a child component happen before the parent's onErrorCaptured hook is registered, so they are not caught.
In this Vue component, errorCount increments each time an error is caught by onErrorCaptured. After three child components throw errors, what is the final value of errorCount?
<script setup> import { ref, onErrorCaptured } from 'vue' const errorCount = ref(0) onErrorCaptured(() => { errorCount.value++ return false }) </script> <template> <ChildOne /> <ChildTwo /> <ChildThree /> </template>
Each error triggers the onErrorCaptured hook once.
Each child throwing an error triggers onErrorCaptured once, incrementing errorCount by 1 each time. After three errors, errorCount is 3.
Choose the correct statement about Vue's onErrorCaptured hook.
Think about error capturing scope and async errors.
onErrorCaptured catches errors from child components synchronously and can stop propagation by returning false. However, errors thrown asynchronously inside child components are not caught by this hook.