You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/reactivity-computed-watchers.md
+3-21Lines changed: 3 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -122,29 +122,9 @@ In this example:
122
122
- The count will be logged synchronously on initial run.
123
123
- When `count` is mutated, the callback will be called **before** the component has updated.
124
124
125
-
Note the first run is executed before the component is mounted. So if you wish to access the DOM (or template refs) in a watched effect, do it in the `onMounted` hook:
125
+
In cases where a watcher effect needs to be re-run **after** component updates, we can pass an additional `options` object with the `flush` option (default is `'pre'`):
126
126
127
127
```js
128
-
onMounted(() => {
129
-
watchEffect(() => {
130
-
// access the DOM or template refs
131
-
})
132
-
})
133
-
```
134
-
135
-
In cases where a watcher effect needs to be re-run synchronously or after component updates, we can pass an additional `options` object with the `flush` option (default is `'pre'`):
136
-
137
-
```js
138
-
// fire synchronously
139
-
watchEffect(
140
-
() => {
141
-
/* ... */
142
-
},
143
-
{
144
-
flush:'sync'
145
-
}
146
-
)
147
-
148
128
// fire after component updates so you can access the updated DOM
149
129
// Note: this will also defer the initial run of the effect until the
150
130
// component's first render is finished.
@@ -158,6 +138,8 @@ watchEffect(
158
138
)
159
139
```
160
140
141
+
The `flush` option also accepts `'sync'`, which forces the effect to always trigger synchronously. This is however inefficient and should be rarely needed.
142
+
161
143
### Watcher Debugging
162
144
163
145
The `onTrack` and `onTrigger` options can be used to debug a watcher's behavior.
0 commit comments