Skip to content

Commit

Permalink
docs: update watch behavior adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 18, 2020
1 parent c05b8e6 commit 190ab87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/guide/migration/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ The following consists a list of breaking changes from 2.x:
- [The `data` option from mixins is now merged shallowly](/guide/migration/data-option.html#mixin-merge-behavior-change)
- [Attributes coercion strategy changed](/guide/migration/attribute-coercion.html)
- [Some transition classes got a rename](/guide/migration/transition.html)
- [Component watch option](/api/options-data.html#watch) and [instance method `$watch`](/api/instance-methods.html#watch) no longer supports dot-delimited string paths, use a computed function as the parameter instead.
- When using [the `watch` option](/api/options-data.html#watch) to watch an array, the callback will only trigger when the array is replaced, and no longer trigger on array mutation. To trigger on mutation, the `deep` option must be specified. This is a side effect of more precise dependency tracking in Vue 3.
- `<template>` tags with no special directives (`v-if/else-if/else`, `v-for`, or `v-slot`) are now treated as plain elements and will result in a native `<template>` element instead of rendering its inner content.
- In Vue 2.x, application root container's `outerHTML` is replaced with root component template (or eventually compiled to a template, if root component has no template/render option). Vue 3.x now uses application container's `innerHTML` instead - this means the container itself is no longer considered part of the template.

Expand Down
12 changes: 7 additions & 5 deletions src/guide/reactivity-computed-watchers.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ An async function implicitly returns a Promise, but the cleanup function needs t

### Effect Flush Timing

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 always invoked after all component `update` effects:
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:

```html
<template>
Expand All @@ -120,7 +120,7 @@ Vue's reactivity system buffers invalidated effects and flushes them asynchronou
In this example:

- The count will be logged synchronously on initial run.
- When `count` is mutated, the callback will be called **after** the component has updated.
- When `count` is mutated, the callback will be called **before** the component has updated.

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:

Expand All @@ -132,7 +132,7 @@ onMounted(() => {
})
```

In cases where a watcher effect needs to be re-run synchronously or before component updates, we can pass an additional `options` object with the `flush` option (default is `'post'`):
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'`):

```js
// fire synchronously
Expand All @@ -145,13 +145,15 @@ watchEffect(
}
)

// fire before component updates
// fire after component updates so you can access the updated DOM
// Note: this will also defer the initial run of the effect until the
// component's first render is finished.
watchEffect(
() => {
/* ... */
},
{
flush: 'pre'
flush: 'post'
}
)
```
Expand Down

0 comments on commit 190ab87

Please sign in to comment.