Vue.js component lifecycle for children
I found this article to be especially helpful in explaining the order of parent/child lifecycle hooks execution. This diagram in particular offers a nice summary of the process.
Also have a look at this post by LinusBorg on the vuejs forum.
beforeCreate()
andcreated()
of the parent run first.- Then the parent’s template is being rendered, which means the child components get created.
- so now the children’s
beforeCreate()
andcreated()
hooks execute respectively.- these child components mount to DOM elements, which calls their
beforeMount()
andmounted()
hooks.- and only then, after the parent’s template has finished, can the parent be mounted to the DOM, so finally the parent’s
beforeMount()
andmounted()
hooks are called.
As the first response give a good view of the creation/mounting process it fails to respond for the destruction process.
here is what happen :
- app created
// we add the parent
- parent Created
- child Created
- child Mounted
- parent Mounted
// now we remove the parent :
- parent ready to unmount
- child ready to unmount
- child unMounted
- parent unMounted
PS : beware that beforeDestroy & destroyed in Vue 2 became beforeUnmounted & unMounted un Vue 3