Unmounting Hooks
Learn and practice the two Vue lifecycle hooks related to object destruction—beforeUnmount and unmounted.
We'll cover the following
Let’s look at the last two regular lifecycle hooks—beforeUnmount
and unmounted
. These two get called right before and right after removing a component respectively. A component is considered destroyed once everything related to the component is gone. This includes reactivity, templates, and child components. Let’s first see how we can add them.
Note: In Vue 2,
beforeUnmount
andunmounted
used to be namedbeforeDestroy
anddestroyed
respectively. WhilebeforeDestroy
anddestroyed
are still around, their usage is discouraged.
Adding the hooks
We can add these hooks the same way we add any other hook. However, one thing should be kept in mind—these hooks won’t be called on the app instance in a typical setup, except if deliberately called via .$destroy()
. However, we can still add them to the app, as illustrated below:
Get hands-on with 1200+ tech skills courses.