Skip to content

Commit 2e34f7d

Browse files
committed
docs: nested routes & route object
1 parent aec32b7 commit 2e34f7d

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed

docs/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- [Installation](installation.md)
44
- [Basic Usage](basic.md)
55
- [Nested Routes](nested.md)
6-
- [Route Context](route.md)
6+
- [Route Object](route.md)
77
- [router-view](view.md)
88
- [v-link](link.md)
99
- [Router Options](options.md)

docs/nested.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,79 @@
11
# Nested Routes
2+
3+
Mapping nested routes to nested components is a common need, and it is also very simple with vue-router.
4+
5+
Suppose we have the following app:
6+
7+
``` html
8+
<div id="app">
9+
<router-view></router-view>
10+
</div>
11+
```
12+
13+
The `<router-view>` here is a top-level outlet. It renders the component matched by a top level route:
14+
15+
``` js
16+
router.map({
17+
'/foo': {
18+
// Foo is rendered when /foo is matched
19+
component: Foo
20+
}
21+
})
22+
```
23+
24+
Similarly, a rendered component can also contain its own, nested `<router-view>`. For example, if we add one inside the `Foo` component's template:
25+
26+
``` js
27+
var Foo = Vue.extend({
28+
template:
29+
'<div class="foo">' +
30+
'<h2>This is Foo!</h2>' +
31+
'<router-view></router-view>' + // <- nested outlet
32+
'</div>'
33+
})
34+
```
35+
36+
To render components into this nested outlet, we need to update our route config:
37+
38+
``` js
39+
router.map({
40+
'/foo': {
41+
component: Foo,
42+
// add a subRoutes map under /foo
43+
subRoutes: {
44+
'/bar': {
45+
// Bar will be rendered inside Foo's <router-view>
46+
// when /foo/bar is matched
47+
component: Bar
48+
},
49+
'/baz': {
50+
// Same for Baz, but only when /foo/baz is matched
51+
component: Baz
52+
}
53+
}
54+
}
55+
})
56+
```
57+
58+
Now, with the above configuration, when you visit `/foo`, nothing will be rendered inside `Foo`'s outlet, because no sub route is matched. Maybe you do want to render something there. In such case you can provide a `/` subroute in this case:
59+
60+
``` js
61+
router.map({
62+
'/foo': {
63+
component: Foo,
64+
subRoutes: {
65+
'/': {
66+
// This component will be rendered into Foo's <router-view>
67+
// when /foo is matched. Using an inline component definition
68+
// here for convenience.
69+
component: {
70+
template: '<p>Default sub view for Foo</p>'
71+
}
72+
},
73+
// other sub routes...
74+
}
75+
}
76+
})
77+
```
78+
79+
A working demo of this example can be found [here](http://jsfiddle.net/yyx990803/naeg67da/).

docs/route.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
1-
# Route Context
1+
# Route Object
2+
3+
Vue-router supports matching paths that contain dynamic segments, star segments and query strings. All these information of a parsed route will be accessible on the exposed **Route Context Objects** (we will just call them "route" objects from now on). The route object will be injected into every component in a vue-router-enabled app as `this.$route`, and will be updated whenever a route transition is performed.
4+
5+
A route object exposes the following properties:
6+
7+
- **$route.path**
8+
9+
A string that equals the path of the current route, always resolved as an absolute path. e.g. `"/foo/bar"`.
10+
11+
- **$route.params**
12+
13+
An object that contains key/value pairs of dynamic segments and star segments. More details below.
14+
15+
- **$route.query**
16+
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+
19+
### Using in Templates
20+
21+
You can directly bind to the `$route` object inside your component templates. For example:
22+
23+
``` html
24+
<div>
25+
<p>Current route path: {{$route.path}}</p>
26+
<p>Current route params: {{$route.params | json}}</p>
27+
</div>
28+
```
29+
30+
### Dynamic Segments
31+
32+
Dynamic segments can be defined in the form of path segments with a leading colon, e.g. in `user/:username`, `:username` is the dynamic segment. It will match paths like `/user/foo` or `/user/bar`. When a path containing a dynamic segment is matched, the dynamic segments will be available inside `$route.params`.
33+
34+
Example Usage:
35+
36+
``` js
37+
router.map({
38+
'/user/:username': {
39+
component: {
40+
template: '<p>username is {{$route.params.username}}</p>'
41+
}
42+
}
43+
})
44+
```
45+
46+
A path can contain multiple dynamic segments, and each of them will be stored as a key/value pair in `$route.params`.
47+
48+
Examples:
49+
50+
| pattern | matched path | $route.params |
51+
|---------|------|--------|
52+
| /user/:username | /user/evan | `{ username: 'evan' }` |
53+
| /user/:username/post/:post_id | /user/evan/post/123 | `{ username: 'evan', post_id: 123 }` |
54+
55+
### Star Segments
56+
57+
While dynamic segments can correspond to only a single segment in a path, star segments is basically the "greedy" version of it. For example `/foo/*bar` will match anything that starts with `/foo/`. The part matched by the star segment will also be available in `$route.params`.
58+
59+
Examples:
60+
61+
| pattern | matched path | $route.params |
62+
|---------|------|--------|
63+
| /user/*any | /user/a/b/c | `{ any: 'a/b/c' }` |
64+
| /foo/*any/bar | /foo/a/b/bar | `{ any: 'a/b' }` |

0 commit comments

Comments
 (0)