Skip to content

Commit 5621d80

Browse files
committed
docs: adapt watch usage [skip ci]
1 parent 47cd7b9 commit 5621d80

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

docs/guide/advanced/data-fetching.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ export default {
3939
}
4040
},
4141
created() {
42-
// fetch the data when the view is created and the data is
43-
// already being observed
44-
this.fetchData()
45-
},
46-
watch: {
47-
// call again the method if the route changes
48-
$route: 'fetchData',
42+
// watch the params of the route to fetch the data again
43+
this.$watch(
44+
() => this.$route.params,
45+
() => {
46+
this.fetchData()
47+
},
48+
// fetch the data when the view is created and the data is
49+
// already being observed
50+
{ immediate: true }
51+
)
4952
},
5053
methods: {
5154
fetchData() {

docs/guide/essentials/dynamic-matching.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ Very often we will need to map routes with the given pattern to the same compone
44

55
```js
66
const User = {
7-
template: '<div>User</div>'
7+
template: '<div>User</div>',
88
}
99

1010
// these are passed to `createRouter`
1111
const routes = [
1212
// dynamic segments start with a colon
13-
{ path: '/user/:id', component: User }
13+
{ path: '/user/:id', component: User },
1414
]
1515
```
1616

@@ -20,7 +20,7 @@ A _param_ is denoted by a colon `:`. When a route is matched, the value of its _
2020

2121
```js
2222
const User = {
23-
template: '<div>User {{ $route.params.id }}</div>'
23+
template: '<div>User {{ $route.params.id }}</div>',
2424
}
2525
```
2626

@@ -41,16 +41,19 @@ In addition to `$route.params`, the `$route` object also exposes other useful in
4141

4242
One thing to note when using routes with params is that when the user navigates from `/user/johnny` to `/user/jolyne`, **the same component instance will be reused**. Since both routes render the same component, this is more efficient than destroying the old instance and then creating a new one. **However, this also means that the lifecycle hooks of the component will not be called**.
4343

44-
To react to params changes in the same component, you can simply watch the `$route` object:
44+
To react to params changes in the same component, you can simply watch anything on the `$route` object, in this scenario, the `$route.params`:
4545

4646
```js
4747
const User = {
4848
template: '...',
49-
watch: {
50-
$route(to, from) {
51-
// react to route changes...
52-
}
53-
}
49+
created() {
50+
this.$watch(
51+
() => this.$route.params,
52+
(toParams, previousParams) => {
53+
// react to route changes...
54+
}
55+
)
56+
},
5457
}
5558
```
5659

@@ -59,10 +62,10 @@ Or, use the `beforeRouteUpdate` [navigation guard](../advanced/navigation-guards
5962
```js
6063
const User = {
6164
template: '...',
62-
beforeRouteUpdate(to, from, next) {
65+
async beforeRouteUpdate(to, from) {
6366
// react to route changes...
64-
// don't forget to call next()
65-
}
67+
this.userData = await fetchUser(to.params.id)
68+
},
6669
}
6770
```
6871

0 commit comments

Comments
 (0)