Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active March 24, 2025 12:41
Show Gist options
  • Save Kcko/b7199eac8eeb75bbb3dd7dff1569fa5b to your computer and use it in GitHub Desktop.
Save Kcko/b7199eac8eeb75bbb3dd7dff1569fa5b to your computer and use it in GitHub Desktop.
// 1) cleanup when watch is ended
import { watch, onWatcherCleanup } from 'vue'
watch(id, (newId) => {
const { response, cancel } = doAsyncWork(newId)
// `cancel` is called if `id` changes or the component unmounts
onWatcherCleanup(cancel)
})
// 2) better watch with desctruct
const { pause, resume, stop } = watch(source, (newVal, oldVal) => {
// Watch logic
});
// Pause watching
pause();
// Resume watching
resume();
// 3 watch logic, not only true/false ...
watch(reactiveObject, (newVal, oldVal) => {
// Watch logic
}, { deep: 2 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment