Hooks

WARNING

Note that these hooks can only be registered in the setup function within defineComponent, or any other hook that is called from there. These hooks will "bind" to the component that's in the current sync "execution stack".

onMounted

declare function onMounted(fn: () => void): void;
1
defineComponent({
  name: 'my-component',
  setup() {
    onMounted(() => {
      // do stuff when the component is mounted
    })
  }
})
1
2
3
4
5
6
7
8

onUnmounted

declare function onUnmounted(fn: () => void): void;
1
defineComponent({
  name: 'my-component',
  setup() {
    onUnmounted(() => {
      // do cleanup stuff right before the component is unmounted
    })
  }
})
1
2
3
4
5
6
7
8