yarn add vue-error-boundary
npm i vue-error-boundary --save
Usage
To use this component simply wrap any other component which may throw an Error. Errors thrown in child components will automatically bubble up to the VErrorBoundary component.
<VErrorBoundary><ImUnstable />
VErrorBoundary>
If you are using it inside a v-for, ensure to set the stop-propagation prop on your VErrorBoundary component.
<template><div>
Could not render - {{ id }}
div>template><script>exportdefault{props: ['id'],}script>
Furthermore, we can directly access the contents of the VErrorBoundary component's errorCaptured hook either through a callback or Vue's emit.
Scoped Slots
If you do not wish to use a fallback component you can alternatively utilize scoped slots to present data in your current template.
Basic Example
No error occurred.
Message to appear if error occurred.
">
<VErrorBoundary><template#boundary="{ hasError }"><divv-if="!hasError">No error occurred.div><divv-else>Message to appear if error occurred.div>template>VErrorBoundary>
Events
Callbacks via on-error
The VErrorBoundary can receive a callback function through the on-error prop.
...
">
<template><VErrorBoundary:on-error="handleError">...VErrorBoundary><template><script>// ...
methods: {handleError(err,vm,info){// do something}}// ...script>
@errorCaptured event
The callback function will receive the same parameters as the errorCaptured method.
We can also listen to a Vue event via an errorCaptured event.
...
">
<template><VErrorBoundary@error-captured="handleError">...VErrorBoundary>template><script>// ...
methods: {handleError(err,vm,info){// do something}}// ...script>
Stop Propagation
The errorCaptured hook will continue to propagate errors up the component tree unless it returns false. Doing so will stop any additional errorCaptured hooks to execute and the global errorHandler from being invoked for the error. To do this we can use the stop-propagation prop.
Hi, i came across this component when looking into error handling in vue. I followed the documentations to setup a project and test its functionality and I believe that the component no longer works.
Totally forgot I left in this.$slots.default[0]. Might want the ability to wrap multiple components at once so we'll want to render all of them. May need to delegate this to a functional component so we can return multiple root elements.
This package is not available to those using strict compilation settings or linting standards. It's not due to a bug in the package itself, but the lack of a definitions file.
I had need of an ErrorBoundary on my project, but am trying to stay inside TypeScript. With the release of the Composition UI, it's a little more possible. I had a go at creating a version of what you've done using it. Thought you might care to use it as a starting point if you begin supporting it or TS:
This is a really nice component, thanks!
It would be nice not to have to create a whole component just for a bit of fallback text/html. Is it possible to just have a slot for the fallback?
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through
GSAP is a robust JavaScript toolset that turns developers into animation superheroes. Build high-performance animations that work in every major browser. Animate CSS, SVG, canvas, React, Vue, WebGL, colors, strings, motion paths, generic objects...anything JavaScript can touch! GSAP's ScrollTrigger plugin lets you create jaw-dropping scroll-based animations with minimal code.