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/essentials/template-refs.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ While Vue's declarative rendering model abstracts away most of the direct DOM op
6
6
<input ref="input">
7
7
```
8
8
9
-
`ref` is a special attribute that is similar to `key`and `is`. It allows us to obtain a direct reference to a specific DOM element or child component instance after it's mounted. This may be useful when you want to, for example, programmatically focus an input on component mount, or initialize a 3rd party library on an element.
9
+
`ref` is a special attribute, similar to the `key`attribute discussed in the `v-for` chapter. It allows us to obtain a direct reference to a specific DOM element or child component instance after it's mounted. This may be useful when you want to, for example, programmatically focus an input on component mount, or initialize a 3rd party library on an element.
@@ -183,8 +181,10 @@ When you call `watch()` directly on a reactive object, it will implicitly create
183
181
```js
184
182
constobj=reactive({ count:0 })
185
183
186
-
watch(obj, () => {
184
+
watch(obj, (newValue, oldValue) => {
187
185
// fires on nested property mutations
186
+
// Note: `newValue` will be equal to `oldValue` here
187
+
// because they both point to the same object!
188
188
})
189
189
190
190
obj.count++
@@ -194,13 +194,26 @@ This should be differentiated with a getter that returns a reactive object - in
194
194
195
195
```js
196
196
watch(
197
-
() =>state.activeObject,
197
+
() =>state.someObject,
198
198
() => {
199
-
// fires only when state.activeObject is replaced
199
+
// fires only when state.someObject is replaced
200
200
}
201
201
)
202
202
```
203
203
204
+
You can, however, force the second case into a deep watcher by explicitly using the `deep` option:
205
+
206
+
```js
207
+
watch(
208
+
() =>state.someObject,
209
+
(newValue, oldValue) => {
210
+
// Note: `newValue` will be equal to `oldValue` here
211
+
// *unless* state.someObject has been replaced
212
+
},
213
+
{ deep:true }
214
+
)
215
+
```
216
+
204
217
</div>
205
218
206
219
:::warning Use with Caution
@@ -265,13 +278,13 @@ You can check out [this example](/examples/#fetching-data) with `watchEffect` an
265
278
266
279
`watch` and `watchEffect` both allow us to reactively perform side effects. Their main difference is the way they track their reactive dependencies:
267
280
268
-
-`watch` only tracks the explicitly watched source. It won't track anything accessed inside the callback. This allows us to have more control over what should trigger the callback.
281
+
-`watch` only tracks the explicitly watched source. It won't track anything accessed inside the callback. In addition, the callback only triggers when the source has actually changed. `watch` separates dependency tracking from the side effect, giving us more precise control over when the callback should fire.
269
282
270
-
-`watchEffect`, on the other hand, automatically tracks every reactive property accessed during its synchronous execution. This is more convenient and typically results in terser code, but makes its reactive dependencies less explicit.
283
+
-`watchEffect`, on the other hand, combines dependency tracking and side effect into one phase. It automatically tracks every reactive property accessed during its synchronous execution. This is more convenient and typically results in terser code, but makes its reactive dependencies less explicit.
271
284
272
285
</div>
273
286
274
-
## Effect Flush Timing
287
+
## Callback Flush Timing
275
288
276
289
Every Vue component has an update function that updates the DOM when relevant state changes. This update function is also a watcher, created by Vue internally.
277
290
@@ -323,7 +336,7 @@ watchPostEffect(() => {
323
336
324
337
<divclass="options-api">
325
338
326
-
## `$watch()`\*
339
+
## `this.$watch()`\*
327
340
328
341
It's also possible to imperatively create watchers using the [`$watch()` instance method](http://localhost:3000/api/component-instance.html#watch):
0 commit comments