Vue & Nuxt Tips
·
1 min read
·
602 views

Vue Tip: Debug Watcher

Michael Hoffmann

Michael Hoffmann

@mokkapps

Vue Tip: Debug Watcher Image

Similar to computed(), watchers also support the onTrack and onTrigger options to debug a watcher's behavior:

Component.vue
<script setup>
watch(source, callback, {
  onTrack(e) {
    debugger
  },
  onTrigger(e) {
    debugger
  },
})

watchEffect(callback, {
  onTrack(e) {
    debugger
  },
  onTrigger(e) {
    debugger
  },
})
</script>

onTrack will be called when a reactive property or ref is tracked as a dependency.

onTrigger will be called when the watcher callback is triggered by the mutation of a dependency.

The callbacks receive a debugger event that contains information about the dependency. If you place a debugger statement inside the callback, you can interactively inspect the dependency.

onTrack and onTrigger watcher options only work in development mode.

If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more.