·
1 min read

Vue Tip: Debug Watcher

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.

Info

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. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:

I will never share any of your personal data. You can unsubscribe at any time.