Skip to content

Commit 62cbcc0

Browse files
committed
docs: matching & nested
1 parent d794785 commit 62cbcc0

File tree

8 files changed

+116
-123
lines changed

8 files changed

+116
-123
lines changed

docs/en/SUMMARY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
- [Installation](installation.md)
77
- Essentials
88
- [Getting Started](essentials/getting-started.md)
9+
- [Dynamic Route Matching](essentials/matching.md)
910
- [Nested Routes](essentials/nested-routes.md)
1011
- [Redirect and Alias](essentials/redirect-and-alias.md)
1112
- [Programmatic Navigation](essentials/navigation.md)
13+
- [Server Config for History Mode](essentials/server.md)
1214
- Advanced
1315
- [Named Routes](advanced/named-routes.md)
1416
- [Named Views](advanced/named-views.md)
@@ -25,4 +27,3 @@
2527
- [Router Instance](api/router-instance.md)
2628
- [The $route Object](api/route-object.md)
2729
- [Component Route Hooks](api/component-hooks.md)
28-
- [Route Matching Rules](api/matching.md)

docs/en/api/matching.md

Whitespace-only changes.

docs/en/essentials/getting-started.md

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Getting Started
22

3-
Creating a Single-page Application with Vue.js + vue-router is dead simple. With Vue.js, we are already dividing our application into components. When adding vue-router to the mix, all we need to do is map our components to the routes and let vue-router know where to render them. Here's a basic example:
3+
> We will be using [ES2015](https://github.com/lukehoban/es6features) in the code samples in the guide.
4+
5+
Creating a Single-page Application with Vue.js + vue-router is dead simple. With Vue.js, we are already composing our application with components. When adding vue-router to the mix, all we need to do is map our components to the routes and let vue-router know where to render them. Here's a basic example:
46

57
### HTML
68

@@ -23,49 +25,40 @@ Creating a Single-page Application with Vue.js + vue-router is dead simple. With
2325
### JavaScript
2426

2527
``` js
26-
// 1. Use plugin.
27-
// This installs <router-view> and <router-link>,
28-
// and injects $router and $route to all router-enabled child components
29-
Vue.use(VueRouter)
28+
// 0. If using a module system, call Vue.use(VueRouter)
3029

31-
// 2. Define route components.
30+
// 1. Define route components.
3231
// These can be imported from other files
33-
var Foo = { template: '<div>foo</div>' }
34-
var Bar = { template: '<div>bar</div>' }
32+
const Foo = { template: '<div>foo</div>' }
33+
const Bar = { template: '<div>bar</div>' }
3534

36-
// 3. Define some routes
35+
// 2. Define some routes
3736
// Each route should map to a component. The "component" can
3837
// either be an actual component constructor created via
3938
// Vue.extend(), or just a component options object.
4039
// We'll talk about nested routes later.
41-
var routes = [
40+
const routes = [
4241
{ path: '/foo', component: Foo },
4342
{ path: '/bar', component: Bar }
4443
]
4544

46-
// 4. Create the router instance and pass the `routes` option
45+
// 3. Create the router instance and pass the `routes` option
4746
// You can pass in additional options here, but let's
4847
// keep it simple for now.
49-
var router = new VueRouter({
50-
routes: routes
48+
const router = new VueRouter({
49+
routes // short for routes: routes
5150
})
5251

53-
// 5. Create and mount the root instance.
52+
// 4. Create and mount the root instance.
5453
// Make sure to inject the router with the router option to make the
5554
// whole app router-aware.
56-
new Vue({
57-
router: router
55+
const app = new Vue({
56+
router
5857
}).$mount('#app')
5958

6059
// Now the app has started!
6160
```
6261

6362
You can also checkout this example [live](http://jsfiddle.net/yyx990803/xgrjzsup/).
6463

65-
In addition:
66-
67-
- Notice that a `<router-link>` automatically gets the `.router-link-active` class when its target route is matched. You can learn more about it in its [API reference](../api/router-link.md).
68-
69-
- The root Vue instance will be available as `router.app` once the initial render is complete. You can learn more about the properties and methods available on the router instance [here](../api/router-instance.md).
70-
71-
- The router instance will be available in all descendants of the root instance as `this.$router`. An object representing the current route state will also be available as [this.$route](../api/route-object.md).
64+
Notice that a `<router-link>` automatically gets the `.router-link-active` class when its target route is matched. You can learn more about it in its [API reference](../api/router-link.md).

docs/en/essentials/matching.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Dynamic Route Matching
2+
3+
Very often we will need to map routes with the given pattern to the same component. For example we may have a `User` component which should be rendered for all users but with different user IDs. In `vue-router` we can use a dynamic segment in the path to achieve that:
4+
5+
``` js
6+
const User = {
7+
template: '<div>User</div>'
8+
}
9+
10+
const router = new VueRouter({
11+
routes: [
12+
// dynamic segments start with a colon
13+
{ path: '/user/:id', component: User }
14+
]
15+
})
16+
```
17+
18+
Now URLs like `/user/foo` and `/user/bar` will both map to the same route.
19+
20+
A dynamic segment is denoted by a colon `:`. When a route is matched, the value of the dynamic segments will be exposed as `this.$route.params` in every component. Therefore, we can render the current user ID by updating `User`'s template to this:
21+
22+
``` js
23+
const User = {
24+
template: '<div>User {{ $route.params.id }}</div>'
25+
}
26+
```
27+
28+
You can checkout a live example [here](http://jsfiddle.net/yyx990803/4xfa2f19/).
29+
30+
You can have multiple dynamic segments in the same route, and they will map to corresponding fields on `$route.params`. Examples:
31+
32+
| pattern | matched path | $route.params |
33+
|---------|------|--------|
34+
| /user/:username | /user/evan | `{ username: 'evan' }` |
35+
| /user/:username/post/:post_id | /user/evan/post/123 | `{ username: 'evan', post_id: 123 }` |
36+
37+
In addition to `$route.params`, the `$route` object also exposes other useful information such as `$route.query` (if there is a query in the URL), `$route.hash`, etc. You can check out the full details in the [API Reference](../api/route-object.md).
38+
39+
### Advanced Matching Patterns
40+
41+
`vue-router` uses [path-to-regexp](https://github.com/pillarjs/path-to-regexp) as its path matching engine, so it supports many advanced matching patterns such as optional dynamic segments, zero or more / one or more requirements, and even custom regex patterns. Check out its [documentation](https://github.com/pillarjs/path-to-regexp#parameters) for these advanced patterns, and [this example](https://github.com/vuejs/vue-router/blob/next/examples/route-matching/app.js) of using them in `vue-router`.
42+
43+
### Matching Priority
44+
45+
Sometimes the same URL may be matched by multiple routes. In such a case the matching priority is determined by the order of route definition: the earlier a route is defined, the higher priority it gets.

docs/en/essentials/nested-routes.md

+42-41
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
11
# Nested Routes
22

3-
Mapping nested routes to nested components is a common need, and it is also very
4-
simple with vue-router.
3+
Real app UIs are usually composed of components that are nested multiple levels deep. It is also very common that the segments of a URL corresponds to a certain structure of nested components, for example:
54

6-
Suppose we have the following app:
5+
```
6+
/user/foo/profile /user/foo/posts
7+
+------------------+ +-----------------+
8+
| User | | User |
9+
| +--------------+ | | +-------------+ |
10+
| | Profile | | +------------> | | Posts | |
11+
| | | | | | | |
12+
| +--------------+ | | +-------------+ |
13+
+------------------+ +-----------------+
14+
```
15+
16+
With `vue-router`, it is very simple to express this relationship using nested route configurations.
17+
18+
Given the app we created in the last chapter:
719

820
``` html
921
<div id="app">
1022
<router-view></router-view>
1123
</div>
1224
```
1325

14-
The `<router-view>` here is a top-level outlet. It renders the component matched
15-
by a top level route:
16-
1726
``` js
18-
var Foo = { template: '<div>Foo</div>' }
27+
const User = {
28+
template: '<div>User {{ $route.params.id }}</div>'
29+
}
30+
1931
const router = new VueRouter({
2032
routes: [
21-
// Foo is rendered when /foo is matched
22-
{
23-
path: '/foo',
24-
component: Foo
25-
}
33+
{ path: '/user/:id', component: User }
2634
]
2735
})
2836
```
2937

30-
Similarly, a rendered component can also contain its own, nested
31-
`<router-view>`. For example, if we add one inside the `Foo` component's
32-
template:
38+
The `<router-view>` here is a top-level outlet. It renders the component matched by a top level route. Similarly, a rendered component can also contain its own, nested `<router-view>`. For example, if we add one inside the `User` component's template:
3339

3440
``` js
35-
var Foo = {
41+
const User = {
3642
template: `
37-
<div class="foo">
38-
<h2>This is Foo!</h2>
43+
<div class="user">
44+
<h2>User {{ $route.params.id }}</h2>
3945
<router-view></router-view>
4046
</div>
4147
`
@@ -48,50 +54,45 @@ option in `VueRouter` constructor config:
4854
``` js
4955
const router = new VueRouter({
5056
routes: [
51-
{
52-
path: '/foo',
53-
component: Foo
57+
{ path: '/user/:id', component: User,
5458
children: [
5559
{
56-
// Bar will be rendered inside Foo's <router-view>
57-
// when /foo/bar is matched
58-
path: '/bar',
59-
component: Bar
60+
// UserProfile will be rendered inside User's <router-view>
61+
// when /user/:id/profile is matched
62+
path: 'profile',
63+
component: UserProfile
6064
},
6165
{
62-
// Bar will be rendered inside Foo's <router-view>
63-
// when /foo/bar is matched
64-
path: '/bar',
65-
component: Bar
66+
// UserPosts will be rendered inside User's <router-view>
67+
// when /user/:id/posts is matched
68+
path: 'posts',
69+
component: UserPosts
6670
}
6771
]
6872
}
6973
]
7074
})
7175
```
7276

73-
As you can see the `children` option has pretty much the same format as the
74-
`routes` option. Therefore, you can keep nesting views as much as you need.
77+
As you can see the `children` option is just another Array of route configuration objects like `routes` itself. Therefore, you can keep nesting views as much as you need.
7578

76-
At this point, with the above configuration, when you visit `/foo`, nothing will be
77-
rendered inside `Foo`'s outlet, because no sub route is matched. Maybe you do
78-
want to render something there. In such case you can provide an empty subroute path:
79+
At this point, with the above configuration, when you visit `/user/foo`, nothing will be rendered inside `User`'s outlet, because no sub route is matched. Maybe you do want to render something there. In such case you can provide an empty subroute path:
7980

8081
``` js
8182
const router = new VueRouter({
8283
routes: [
8384
{
84-
path: '/foo',
85-
component: Foo
85+
path: '/user/:id', component: User,
8686
children: [
87-
// Default will be rendered inside Foo's <router-view>
88-
// when /foo is matched
89-
{ path: '', component: Default },
90-
// other sub routes
87+
// UserHome will be rendered inside User's <router-view>
88+
// when /user/:id is matched
89+
{ path: '', component: UserHome },
90+
91+
// ...other sub routes
9192
]
9293
}
9394
]
9495
})
9596
```
9697

97-
A working demo of this example can be found [here](https://jsfiddle.net/posva/wuczg0av/).
98+
A working demo of this example can be found [here](http://jsfiddle.net/yyx990803/L7hscd8h/).
File renamed without changes.

docs/en/installation.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
### Direct Download / CDN
44

5-
Unpkg.com provides NPM-based CDN links. The latest release is available at [https://unpkg.com/vue-router@latest/dist/vue-router.js](https://unpkg.com/vue-router@latest/dist/vue-router.js) - you can also specify any other versions/tags available on NPM!
5+
[https://unpkg.com/vue-router](https://unpkg.com/vue-router)
6+
7+
[Unpkg.com](https://unpkg.com) provides NPM-based CDN links. The above link will always point to the latest release on NPM. You can also use a specific version/tag via URLs like `https://unpkg.com/[email protected]`.
8+
9+
Include `vue-router` after Vue and it will install itself automatically:
10+
11+
``` html
12+
<script src="/path/to/vue.js"></script>
13+
<script src="/path/to/vue-router.js"></script>
14+
```
615

716
### NPM
817

@@ -19,8 +28,7 @@ import VueRouter from 'vue-router'
1928
Vue.use(VueRouter)
2029
```
2130

22-
You don't need to do this when using the standalone build because it installs
23-
itself automatically.
31+
You don't need to do this when using global script tags.
2432

2533
### Dev Build
2634

docs/en/options.md

-55
This file was deleted.

0 commit comments

Comments
 (0)