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: docs/en/route.md
+49-21Lines changed: 49 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,46 +16,49 @@ A route object exposes the following properties:
16
16
17
17
An object that contains key/value pairs of the query string. For example, for a path `/foo?user=1`, we get `$route.query.user == 1`.
18
18
19
-
-**$route.router**
20
-
21
-
The router instance that is managing this route (and its owner component).
22
-
23
19
-**$route.matched**
24
20
25
21
An array containing the route configuration objects for all matched segments in the current route.
26
22
27
23
-**$route.name**
28
24
29
-
The name of the current route, if it has one. (See [named routes](./named.md))
25
+
The name of the current (deepest) matched route, if it has one. (See [named routes](./named.md))
26
+
27
+
-**$route.meta**
28
+
29
+
The meta of the current (deepest) matched route, if defined. More details below.
30
30
31
-
### Custom Fields
31
+
### Meta Field
32
32
33
-
In addition to the built-in properties, custom fields defined in the route configwill also be merged on to the route object. For example:
33
+
In addition to the built-in properties, custom fields can be defined under `meta` field in the config, and will be exposed on the route object. For example:
34
34
35
35
```js
36
-
router.map({
37
-
'/a': {
36
+
var router =newVueRouter({
37
+
routes: [{
38
+
path:'/a'
38
39
component: { ... },
39
-
auth:true
40
-
}
40
+
meta: {
41
+
auth:true
42
+
}
43
+
}]
41
44
})
42
45
```
43
46
44
-
When `/a` is matched, `$route.auth` will be `true`. This allows us to perform authentication checks in global hooks:
47
+
When `/a` is matched, `$route.meta.auth` will be `true`. This allows us to perform authentication checks in global hooks:
if (route.matched.some(m=>m.meta.auth)&&!authenticated) {
52
+
redirect('/login')
50
53
} else {
51
-
transition.next()
54
+
next()
52
55
}
53
56
})
54
57
```
55
58
56
59
> See [API](api/before-each.md) for how the `beforeEach` hook works.
57
60
58
-
When a nested route is matched, all custom fields will be merged on to the same `$route` object. When a sub route and a parent route has the same custom field, the sub route's value will overwrite the parent's.
61
+
Note that in the example above, we checked `route.matched` instead of just `route.meta.auth`. This is because when a nested route is matched, only the deepest matched sub route's meta will be exposed on `route`. To get the `meta`s of all the matched routes, we need to iterate through `route.matched`.
59
62
60
63
### Using in Templates
61
64
@@ -64,7 +67,8 @@ You can directly bind to the `$route` object inside your component templates. Fo
0 commit comments