javascript / beginner
Snippet
Watching for Changes
The watch function allows you to perform side effects (like API calls or logging) in response to changes in a reactive data source.
snippet.js
javascript
1
2
3
4
5
import { watch } from 'vue';watch(source, (newValue, oldValue) => {console.log('Value changed from', oldValue, 'to', newValue);});
vue
Breakdown
1
watch(source, ...)
Specifies which variable or source the function should keep an eye on.
2
(newValue, oldValue) => { ... }
The function that runs whenever the source changes, providing both the current and previous values.