LambdaLuke Help

Lifecycle Hooks

Lifecycle hooks allow for code to run at different stages of an applications lifecycle.

Lifecycle hooks need to be imported from vue -

import {onBeforeMount, onMounted, onBeforeUnmount, onUnmounted} from "vue"

Mounted Hooks

  • Before Mount

Code will run just before the component is loaded to the browser

onBeforeMount(() => { console.log("beforeMount") })
  • On Mount

Code will run when the component is loaded

onMounted(() => { console.log("onMounted") })
  • Before Mount

Code will run just before the component is removed from the browser

onBeforeUnmount(() => { console.log("onBeforeUnmount") })
  • On Unmount

Code will run after component has been removed from the browser

onUnmounted(() => { console.log("onUnmounted") })

Activated Hooks

In order to use these hooks a 'keep alive' tag needs to be used in the main template where the router is used -

To achieve this replace this -

<RouterView />

With this -

<router-view v-slot="{ Component }"> <keep-alive> <component :is="Component" /> </keep-alive> </router-view>
  • Activated Hook

This hook will only run if the component is being actively kept alive

onActivated(() => { console.log("onActivated") })
  • Deactivated Hook

This hook will run when the component is no longer being kept alive

onDeactivated(() => { console.log("onDeactivated") })

Updated Hooks

These hooks run whenever a component is updated.

  • Before Update

This hook will run when a component is updated just before the change occurs on screen

onBeforeUpdate(() => { console.log('onBeforeUpdate') })
  • Updated

This hook will run the moment a component is updated

onUpdated(() => { console.log('onUpdated') })
Last modified: 23 September 2024