Skip to content

Commit 82d6bef

Browse files
committed
docs: fetching and meta
1 parent e03cfba commit 82d6bef

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

docs/guide/advanced/data-fetching.md

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ Let's assume we have a `Post` component that needs to fetch the data for a post
1717
```html
1818
<template>
1919
<div class="post">
20-
<div v-if="loading" class="loading">
21-
Loading...
22-
</div>
20+
<div v-if="loading" class="loading">Loading...</div>
2321

24-
<div v-if="error" class="error">
25-
{{ error }}
26-
</div>
22+
<div v-if="error" class="error">{{ error }}</div>
2723

2824
<div v-if="post" class="content">
2925
<h2>{{ post.title }}</h2>
@@ -39,7 +35,7 @@ export default {
3935
return {
4036
loading: false,
4137
post: null,
42-
error: null
38+
error: null,
4339
}
4440
},
4541
created() {
@@ -49,7 +45,7 @@ export default {
4945
},
5046
watch: {
5147
// call again the method if the route changes
52-
$route: 'fetchData'
48+
$route: 'fetchData',
5349
},
5450
methods: {
5551
fetchData() {
@@ -64,8 +60,8 @@ export default {
6460
this.post = post
6561
}
6662
})
67-
}
68-
}
63+
},
64+
},
6965
}
7066
```
7167

@@ -79,7 +75,7 @@ export default {
7975
data() {
8076
return {
8177
post: null,
82-
error: null
78+
error: null,
8379
}
8480
},
8581
beforeRouteEnter(to, from, next) {
@@ -89,27 +85,19 @@ export default {
8985
},
9086
// when route changes and this component is already rendered,
9187
// the logic will be slightly different.
92-
beforeRouteUpdate(to, from, next) {
88+
async beforeRouteUpdate(to, from) {
9389
this.post = null
94-
getPost(to.params.id, (err, post) => {
95-
this.setData(err, post)
96-
next()
97-
})
98-
},
99-
methods: {
100-
setData(err, post) {
101-
if (err) {
102-
this.error = err.toString()
103-
} else {
104-
this.post = post
105-
}
90+
try {
91+
this.post = await getPost(to.params.id)
92+
} catch (error) {
93+
this.error = error.toString()
10694
}
107-
}
95+
},
10896
}
10997
```
11098

11199
The user will stay on the previous view while the resource is being fetched for the incoming view. It is therefore recommended to display a progress bar or some kind of indicator while the data is being fetched. If the data fetch fails, it's also necessary to display some kind of global warning message.
112100

113-
### Using Composition API
101+
<!-- ### Using Composition API -->
114102

115-
TODO:
103+
<!-- TODO: -->

docs/guide/advanced/meta.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,41 @@ const routes = [
2727

2828
So how do we access this `meta` field?
2929

30+
<!-- TODO: the explanation about route records should be explained before and things should be moved here -->
31+
3032
First, each route object in the `routes` configuration is called a **route record**. Route records may be nested. Therefore when a route is matched, it can potentially match more than one route record.
3133

3234
For example, with the above route config, the URL `/posts/new` will match both the parent route record (`path: '/posts'`) and the child route record (`path: 'new'`).
3335

34-
All route records matched by a route are exposed on the `$route` object (and also route objects in navigation guards) as the `$route.matched` Array. We could loop through that array to check all `meta` fields, but Vue Router also provides you a `$route.meta` that is first-level merge of **all `meta`** fields from parent to child. Meaning you can simply write
36+
All route records matched by a route are exposed on the `$route` object (and also route objects in navigation guards) as the `$route.matched` Array. We could loop through that array to check all `meta` fields, but Vue Router also provides you a `$route.meta` that is a non-recursive merge of **all `meta`** fields from parent to child. Meaning you can simply write
3537

3638
```js
37-
router.beforeEach((to, from, next) => {
39+
router.beforeEach((to, from) => {
3840
// instead of having to check every route record with
3941
// to.matched.some(record => record.meta.requiresAuth)
40-
if (to.meta.requiresAuth)) {
42+
if (to.meta.requiresAuth && !auth.isLoggedIn()) {
4143
// this route requires auth, check if logged in
4244
// if not, redirect to login page.
43-
if (!auth.isLoggedIn()) {
44-
next({
45-
path: '/login',
46-
// save the ___location we were at to come back later
47-
query: { redirect: to.fullPath }
48-
})
49-
} else {
50-
next()
45+
return {
46+
path: '/login',
47+
// save the ___location we were at to come back later
48+
query: { redirect: to.fullPath },
5149
}
52-
} else {
53-
next() // make sure to always call next()!
5450
}
5551
})
5652
```
53+
54+
## TypeScript
55+
56+
It is possible to type the meta field by extending the `RouteMeta` interface:
57+
58+
```ts
59+
declare module 'vue-router' {
60+
interface RouteMeta {
61+
// is optional
62+
isAdmin?: boolean
63+
// must be declared by every route
64+
requiresAuth: boolean
65+
}
66+
}
67+
```

0 commit comments

Comments
 (0)