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
+79-1Lines changed: 79 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -96,6 +96,7 @@ An async function implicitly returns a Promise, but the cleanup function needs t
96
96
Vue's reactivity system buffers invalidated effects and flushes them asynchronously to avoid unnecessary duplicate invocation when there are many state mutations happening in the same "tick". Internally, a component's `update` function is also a watched effect. When a user effect is queued, it is by default invoked **before** all component `update` effects:
97
97
98
98
```html
99
+
99
100
<template>
100
101
<div>{{ count }}</div>
101
102
</template>
@@ -201,7 +202,7 @@ A watcher can also watch multiple sources at the same time using an array:
However, watching a reactive object or array will always return a reference to the current value of that object for both the current and previous value of the state. To fully watch deeply nested objects and arrays, a deep copy of values may be required. This can be achieved with a utility such as [lodash.cloneDeep](https://lodash.com/docs/4.17.15#cloneDeep)
268
+
269
+
```js
270
+
import_from'lodash';
271
+
272
+
conststate=reactive({
273
+
id:1,
274
+
attributes: {
275
+
name:"",
276
+
},
277
+
});
278
+
279
+
watch(
280
+
() =>_.cloneDeep(state),
281
+
(state, prevState) => {
282
+
console.log(
283
+
state.attributes.name,
284
+
prevState.attributes.name
285
+
);
286
+
}
287
+
);
288
+
289
+
state.attributes.name="Alex"; // Logs: "Alex" ""
290
+
```
291
+
214
292
### Shared Behavior with `watchEffect`
215
293
216
294
`watch` shares behavior with [`watchEffect`](#watcheffect) in terms of [manual stoppage](#stopping-the-watcher), [side effect invalidation](#side-effect-invalidation) (with `onInvalidate` passed to the callback as the 3rd argument instead), [flush timing](#effect-flush-timing) and [debugging](#watcher-debugging).
0 commit comments