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
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:
4
6
5
7
### HTML
6
8
@@ -23,49 +25,40 @@ Creating a Single-page Application with Vue.js + vue-router is dead simple. With
23
25
### JavaScript
24
26
25
27
```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)
30
29
31
-
//2. Define route components.
30
+
//1. Define route components.
32
31
// These can be imported from other files
33
-
var Foo = { template:'<div>foo</div>' }
34
-
var Bar = { template:'<div>bar</div>' }
32
+
constFoo= { template:'<div>foo</div>' }
33
+
constBar= { template:'<div>bar</div>' }
35
34
36
-
//3. Define some routes
35
+
//2. Define some routes
37
36
// Each route should map to a component. The "component" can
38
37
// either be an actual component constructor created via
39
38
// Vue.extend(), or just a component options object.
40
39
// We'll talk about nested routes later.
41
-
var routes = [
40
+
constroutes= [
42
41
{ path:'/foo', component: Foo },
43
42
{ path:'/bar', component: Bar }
44
43
]
45
44
46
-
//4. Create the router instance and pass the `routes` option
45
+
//3. Create the router instance and pass the `routes` option
47
46
// You can pass in additional options here, but let's
48
47
// keep it simple for now.
49
-
var router =newVueRouter({
50
-
routes: routes
48
+
constrouter=newVueRouter({
49
+
routes// short for routes: routes
51
50
})
52
51
53
-
//5. Create and mount the root instance.
52
+
//4. Create and mount the root instance.
54
53
// Make sure to inject the router with the router option to make the
55
54
// whole app router-aware.
56
-
newVue({
57
-
router: router
55
+
constapp=newVue({
56
+
router
58
57
}).$mount('#app')
59
58
60
59
// Now the app has started!
61
60
```
62
61
63
62
You can also checkout this example [live](http://jsfiddle.net/yyx990803/xgrjzsup/).
64
63
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).
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
+
constUser= {
7
+
template:'<div>User</div>'
8
+
}
9
+
10
+
constrouter=newVueRouter({
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
+
constUser= {
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:
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.
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:
5
4
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:
7
19
8
20
```html
9
21
<divid="app">
10
22
<router-view></router-view>
11
23
</div>
12
24
```
13
25
14
-
The `<router-view>` here is a top-level outlet. It renders the component matched
15
-
by a top level route:
16
-
17
26
```js
18
-
var Foo = { template:'<div>Foo</div>' }
27
+
constUser= {
28
+
template:'<div>User {{ $route.params.id }}</div>'
29
+
}
30
+
19
31
constrouter=newVueRouter({
20
32
routes: [
21
-
// Foo is rendered when /foo is matched
22
-
{
23
-
path:'/foo',
24
-
component: Foo
25
-
}
33
+
{ path:'/user/:id', component: User }
26
34
]
27
35
})
28
36
```
29
37
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:
33
39
34
40
```js
35
-
var Foo= {
41
+
constUser= {
36
42
template:`
37
-
<div class="foo">
38
-
<h2>This is Foo!</h2>
43
+
<div class="user">
44
+
<h2>User {{ $route.params.id }}</h2>
39
45
<router-view></router-view>
40
46
</div>
41
47
`
@@ -48,50 +54,45 @@ option in `VueRouter` constructor config:
48
54
```js
49
55
constrouter=newVueRouter({
50
56
routes: [
51
-
{
52
-
path:'/foo',
53
-
component: Foo
57
+
{ path:'/user/:id', component: User,
54
58
children: [
55
59
{
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
60
64
},
61
65
{
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
66
70
}
67
71
]
68
72
}
69
73
]
70
74
})
71
75
```
72
76
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.
75
78
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:
79
80
80
81
```js
81
82
constrouter=newVueRouter({
82
83
routes: [
83
84
{
84
-
path:'/foo',
85
-
component: Foo
85
+
path:'/user/:id', component: User,
86
86
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
91
92
]
92
93
}
93
94
]
94
95
})
95
96
```
96
97
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/).
Copy file name to clipboardExpand all lines: docs/en/installation.md
+11-3
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,16 @@
2
2
3
3
### Direct Download / CDN
4
4
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!
[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
+
<scriptsrc="/path/to/vue.js"></script>
13
+
<scriptsrc="/path/to/vue-router.js"></script>
14
+
```
6
15
7
16
### NPM
8
17
@@ -19,8 +28,7 @@ import VueRouter from 'vue-router'
19
28
Vue.use(VueRouter)
20
29
```
21
30
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.
0 commit comments