Skip to content

Commit a09c316

Browse files
docs: add the flush option for $watch (#605)
1 parent 998a71e commit a09c316

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/api/instance-methods.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- `{Object} options (optional)`
1010
- `{boolean} deep`
1111
- `{boolean} immediate`
12+
- `{string} flush`
1213

1314
- **Returns:** `{Function} unwatch`
1415

@@ -172,6 +173,24 @@
172173
)
173174
```
174175

176+
- **Option: flush**
177+
178+
The `flush` option allows for greater control over the timing of the callback. It can be set to `'pre'`, `'post'` or `'sync'`.
179+
180+
The default value is `'pre'`, which specifies that the callback should be invoked before rendering. This allows the callback to update other values before the template runs.
181+
182+
The value `'post'` can be used to defer the callback until after rendering. This should be used if the callback needs access to the updated DOM or child components via `$refs`.
183+
184+
If `flush` is set to `'sync'`, the callback will be called synchronously, as soon as the value changes.
185+
186+
For both `'pre'` and `'post'`, the callback is buffered using a queue. The callback will only be added to the queue once, even if the watched value changes multiple times. The interim values will be skipped and won't be passed to the callback.
187+
188+
Buffering the callback not only improves performance but also helps to ensure data consistency. The watchers won't be triggered until the code performing the data updates has finished.
189+
190+
`'sync'` watchers should be used sparingly, as they don't have these benefits.
191+
192+
For more information about `flush` see [Effect Flush Timing](../guide/reactivity-computed-watchers.html#effect-flush-timing).
193+
175194
- **See also:** [Watchers](../guide/computed.html#watchers)
176195

177196
## $emit

src/api/options-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177

178178
- **Details:**
179179

180-
An object where keys are expressions to watch and values are the corresponding callbacks. The value can also be a string of a method name, or an Object that contains additional options. The component instance will call `$watch()` for each entry in the object at instantiation.
180+
An object where keys are expressions to watch and values are the corresponding callbacks. The value can also be a string of a method name, or an Object that contains additional options. The component instance will call `$watch()` for each entry in the object at instantiation. See [$watch](instance-methods.html#watch) for more information about the `deep`, `immediate` and `flush` options.
181181

182182
- **Example:**
183183

0 commit comments

Comments
 (0)