|
9 | 9 | - `{Object} options (optional)` |
10 | 10 | - `{boolean} deep` |
11 | 11 | - `{boolean} immediate` |
| 12 | + - `{string} flush` |
12 | 13 |
|
13 | 14 | - **Returns:** `{Function} unwatch` |
14 | 15 |
|
15 | 16 | - **Usage:** |
16 | 17 |
|
17 | | - Watch a reactive property or a computed function on the component instance for changes. The callback gets called with the new value and the old value for the given property. We can only pass top-level `data`, `prop`, or `computed` property name as a string. For more complex expressions or nested properties, use a function instead. |
| 18 | + Watch a reactive property or a computed function on the component instance for changes. The callback gets called with the new value and the old value for the given property. We can only pass top-level `data`, `props`, or `computed` property name as a string. For more complex expressions or nested properties, use a function instead. |
18 | 19 |
|
19 | 20 | - **Example:** |
20 | 21 |
|
21 | 22 | ```js |
22 | | - const app = Vue.createApp({ |
| 23 | + const app = createApp({ |
23 | 24 | data() { |
24 | 25 | return { |
25 | 26 | a: 1, |
|
58 | 59 | }) |
59 | 60 | ``` |
60 | 61 |
|
61 | | - When watched value is an Object or Array, any changes to its properties or elements won't trigger the watcher because they reference the same Object/Array: |
| 62 | + When watched value is an object or array, any changes to its properties or elements won't trigger the watcher because they reference the same object/array: |
62 | 63 |
|
63 | 64 | ```js |
64 | | - const app = Vue.createApp({ |
| 65 | + const app = createApp({ |
65 | 66 | data() { |
66 | 67 | return { |
67 | 68 | article: { |
|
80 | 81 | }) |
81 | 82 | }, |
82 | 83 | methods: { |
83 | | - // These methods won't trigger a watcher because we changed only a property of Object/Array, |
84 | | - // not the Object/Array itself |
| 84 | + // These methods won't trigger a watcher because we changed only a property of object/array, |
| 85 | + // not the object/array itself |
85 | 86 | changeArticleText() { |
86 | 87 | this.article.text = 'Vue 3 is awesome' |
87 | 88 | }, |
88 | 89 | addComment() { |
89 | 90 | this.comments.push('New comment') |
90 | 91 | }, |
91 | 92 |
|
92 | | - // These methods will trigger a watcher because we replaced Object/Array completely |
| 93 | + // These methods will trigger a watcher because we replaced object/array completely |
93 | 94 | changeWholeArticle() { |
94 | 95 | this.article = { text: 'Vue 3 is awesome' } |
95 | 96 | }, |
|
103 | 104 | `$watch` returns an unwatch function that stops firing the callback: |
104 | 105 |
|
105 | 106 | ```js |
106 | | - const app = Vue.createApp({ |
| 107 | + const app = createApp({ |
107 | 108 | data() { |
108 | 109 | return { |
109 | 110 | a: 1 |
|
120 | 121 |
|
121 | 122 | - **Option: deep** |
122 | 123 |
|
123 | | - To also detect nested value changes inside Objects, you need to pass in `deep: true` in the options argument. Note that you don't need to do so to listen for Array mutations. |
| 124 | + To also detect nested value changes inside Objects, you need to pass in `deep: true` in the options argument. This option also can be used to watch array mutations. |
| 125 | + |
| 126 | + > Note: when mutating (rather than replacing) an Object or an Array and watch with deep option, the old value will be the same as new value because they reference the same Object/Array. Vue doesn't keep a copy of the pre-mutate value. |
124 | 127 |
|
125 | 128 | ```js |
126 | 129 | vm.$watch('someObject', callback, { |
|
158 | 161 | If you still want to call an unwatch function inside the callback, you should check its availability first: |
159 | 162 |
|
160 | 163 | ```js |
161 | | - const unwatch = vm.$watch( |
| 164 | + let unwatch = null |
| 165 | + |
| 166 | + unwatch = vm.$watch( |
162 | 167 | 'value', |
163 | 168 | function() { |
164 | 169 | doSomething() |
|
170 | 175 | ) |
171 | 176 | ``` |
172 | 177 |
|
| 178 | +- **Option: flush** |
| 179 | + |
| 180 | + The `flush` option allows for greater control over the timing of the callback. It can be set to `'pre'`, `'post'` or `'sync'`. |
| 181 | + |
| 182 | + 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. |
| 183 | + |
| 184 | + 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`. |
| 185 | + |
| 186 | + If `flush` is set to `'sync'`, the callback will be called synchronously, as soon as the value changes. |
| 187 | + |
| 188 | + 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. |
| 189 | + |
| 190 | + 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. |
| 191 | + |
| 192 | + `'sync'` watchers should be used sparingly, as they don't have these benefits. |
| 193 | + |
| 194 | + For more information about `flush` see [Effect Flush Timing](../guide/reactivity-computed-watchers.html#effect-flush-timing). |
| 195 | + |
173 | 196 | - **See also:** [Watchers](../guide/computed.html#watchers) |
174 | 197 |
|
175 | 198 | ## $emit |
|
272 | 295 | - **Example:** |
273 | 296 |
|
274 | 297 | ```js |
275 | | - Vue.createApp({ |
| 298 | + createApp({ |
276 | 299 | // ... |
277 | 300 | methods: { |
278 | 301 | // ... |
|
0 commit comments