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: source/api/instance-methods.md
+24-9Lines changed: 24 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -7,33 +7,48 @@ order: 4
7
7
8
8
> You can observe data changes on a Vue instance. Note that all watch callbacks fire asynchronously. In addition, value changes are batched within an event loop. This means when a value changes multiple times within a single event loop, the callback will be fired only once with the latest value.
Watch an expression on the Vue instance for changes. The expression can be a single keypath or actual expressions:
19
+
Watch an expression or a computed function on the Vue instance for changes. The expression can be a single keypath or actual expressions:
18
20
19
21
```js
20
22
vm.$watch('a + b', function (newVal, oldVal) {
21
23
// do something
22
24
})
25
+
// or
26
+
vm.$watch(
27
+
function () {
28
+
returnthis.a+this.b
29
+
},
30
+
function (newVal, oldVal) {
31
+
// do something
32
+
}
33
+
)
23
34
```
24
35
25
-
To also detect nested value changes inside Objects, you need to pass in `true`for the third `deep` argument. Note that you don't need to do so to listen for Array mutations.
36
+
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.
26
37
27
38
```js
28
-
vm.$watch('someObject', callback, true)
39
+
vm.$watch('someObject', callback, {
40
+
deep:true
41
+
})
29
42
vm.someObject.nestedValue=123
30
43
// callback is fired
31
44
```
32
45
33
-
Passing in `true`for the fourth `immediate` argument will trigger the callback immediately with the current value of the expression:
46
+
Passing in `immediate: true`in the option will trigger the callback immediately with the current value of the expression:
34
47
35
48
```js
36
-
vm.$watch('a', callback, false, true)
49
+
vm.$watch('a', callback, {
50
+
immediate:true
51
+
})
37
52
// callback is fired immediately with current value of `a`
0 commit comments