Skip to content

Commit 03bb7a9

Browse files
committed
finish updating docs/en/route.md
1 parent a047c45 commit 03bb7a9

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

docs/en/route.md

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,49 @@ A route object exposes the following properties:
1616

1717
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`.
1818

19-
- **$route.router**
20-
21-
The router instance that is managing this route (and its owner component).
22-
2319
- **$route.matched**
2420

2521
An array containing the route configuration objects for all matched segments in the current route.
2622

2723
- **$route.name**
2824

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.
3030

31-
### Custom Fields
31+
### Meta Field
3232

33-
In addition to the built-in properties, custom fields defined in the route config will 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:
3434

3535
``` js
36-
router.map({
37-
'/a': {
36+
var router = new VueRouter({
37+
routes: [{
38+
path: '/a'
3839
component: { ... },
39-
auth: true
40-
}
40+
meta: {
41+
auth: true
42+
}
43+
}]
4144
})
4245
```
4346

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:
4548

4649
``` js
47-
router.beforeEach(function (transition) {
48-
if (transition.to.auth && !authenticated) {
49-
transition.redirect('/login')
50+
router.beforeEach(function(route, redirect, next) {
51+
if (route.matched.some(m => m.meta.auth) && !authenticated) {
52+
redirect('/login')
5053
} else {
51-
transition.next()
54+
next()
5255
}
5356
})
5457
```
5558

5659
> See [API](api/before-each.md) for how the `beforeEach` hook works.
5760
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`.
5962

6063
### Using in Templates
6164

@@ -64,7 +67,8 @@ You can directly bind to the `$route` object inside your component templates. Fo
6467
``` html
6568
<div>
6669
<p>Current route path: {{$route.path}}</p>
67-
<p>Current route params: {{$route.params | json}}</p>
70+
<!-- `stringify` is a method provided by you -->
71+
<p>Current route params: {{stringify($route.params)}}</p>
6872
</div>
6973
```
7074

@@ -77,12 +81,13 @@ Dynamic segments can be defined in the form of path segments with a leading colo
7781
Example Usage:
7882

7983
``` js
80-
router.map({
81-
'/user/:username': {
84+
var router = new VueRouter({
85+
routes: [{
86+
path: '/user/:username',
8287
component: {
8388
template: '<p>username is {{$route.params.username}}</p>'
8489
}
85-
}
90+
}]
8691
})
8792
```
8893

@@ -105,3 +110,26 @@ Examples:
105110
|---------|------|--------|
106111
| /user/*any | /user/a/b/c | `{ any: 'a/b/c' }` |
107112
| /foo/*any/bar | /foo/a/b/bar | `{ any: 'a/b' }` |
113+
114+
#### Matching precedence
115+
The routes defined inside the array follows a simple rule of precedence: the latter overrides the former.
116+
117+
Example:
118+
``` js
119+
var router = new VueRouter({
120+
routes: [{
121+
path: '/user/:username',
122+
component: {
123+
template: '<p>username is {{$route.params.username}}</p>'
124+
}
125+
}, {
126+
path: '/user/foo',
127+
component: {
128+
template: '<p>This page is only for user Foo!</p>'
129+
}
130+
}]
131+
})
132+
133+
// Now '/user/foo' shows 'This page is only for user Foo!'
134+
// instead of 'username is foo'
135+
```

0 commit comments

Comments
 (0)