Skip to content

Commit 3b39bc8

Browse files
committed
docs: api
1 parent 708d80b commit 3b39bc8

21 files changed

+241
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# vue-router [![Build Status](https://img.shields.io/circleci/project/vuejs/vue-router/master.svg)](https://circleci.com/gh/vuejs/vue-router)
1+
# vue-router [![Build Status](https://img.shields.io/circleci/project/vuejs/vue-router/master.svg)](https://circleci.com/gh/vuejs/vue-router) [![npm package](https://img.shields.io/npm/v/vue-router.svg)](https://www.npmjs.com/package/vue-router)
22

33
**Compatibility Note:** `vue-router` requires Vue.js 0.12.10+
44

docs/SUMMARY.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# vue-router documentation
1+
# vue-router documentation [![npm package](https://img.shields.io/npm/v/vue-router.svg)](https://www.npmjs.com/package/vue-router)
22

33
- [Installation](installation.md)
44
- [Basic Usage](basic.md)
@@ -17,12 +17,12 @@
1717
- [canReuse](pipeline/can-reuse.md)
1818
- [API Reference](api/README.md)
1919
- [Router instance properties](api/properties.md)
20+
- [router.start](api/start.md)
21+
- [router.stop](api/stop.md)
2022
- [router.map](api/map.md)
2123
- [router.on](api/on.md)
24+
- [router.go](api/go.md)
25+
- [router.replace](api/replace.md)
2226
- [router.redirect](api/redirect.md)
2327
- [router.alias](api/alias.md)
2428
- [router.beforeEach](api/before-each.md)
25-
- [router.go](api/go.md)
26-
- [router.replace](api/replace.md)
27-
- [router.start](api/start.md)
28-
- [router.stop](api/stop.md)

docs/api/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# API Reference
2+
3+
- [Router instance properties](properties.md)
4+
- [router.start](start.md)
5+
- [router.stop](stop.md)
6+
- [router.map](map.md)
7+
- [router.on](on.md)
8+
- [router.go](go.md)
9+
- [router.replace](replace.md)
10+
- [router.redirect](redirect.md)
11+
- [router.alias](alias.md)
12+
- [router.beforeEach](before-each.md)

docs/api/alias.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# `router.alias(aliasMap)`
2+
3+
Configures global alias rules for the router. The difference between alias and redirect is that instead of replacing the `fromPath` with `toPath`, an alias will preserve `fromPath` while matching it as `toPath`.
4+
5+
For example, if we alias `/a` to `/a/b/c`, when we visit `/a`, the browser URL will display `/a`. However, the router will match the path as if we are visiting `/a/b/c` instead.
6+
7+
### Arguments
8+
9+
- `aliasMap {Object}`
10+
11+
The alias map object should be in the form of { fromPath: toPath, ... }. The paths can contain dynamic segments.
12+
13+
### Example
14+
15+
``` js
16+
router.alias({
17+
18+
// match /a as if it is /a/b/c
19+
'/a': '/a/b/c',
20+
21+
// alias can contian dynamic segments
22+
// the dynamic segment names must match
23+
'/user/:userId': '/user/profile/:userId'
24+
})
25+
```

docs/api/before-each.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# `router.beforeEach(hook)`
2+
3+
Set the global before hook, which will be called before every route transition starts. This is before the entire transition pipeline; if the hook rejects the transition, the pipeline won't even be started.
4+
5+
Note you can only have one global before hook at a time; however you can implement your own middleware system inside this hook.
6+
7+
### Arguments
8+
9+
- `hook {Function}`
10+
11+
The hook function receives a single argument which is a [Transition Object](../pipeline/hooks.html#transition-object).
12+
13+
### Example
14+
15+
Basic
16+
17+
``` js
18+
router.beforeEach(function (transition) {
19+
if (transition.to.path === '/forbidden') {
20+
transition.abort()
21+
} else {
22+
transition.next()
23+
}
24+
})
25+
```
26+
27+
Promise + ES6
28+
29+
``` js
30+
router.beforeEach(function ({ to, next }) {
31+
if (to.path === '/auth-required') {
32+
// return a Promise that resolves to true or false
33+
return AuthService.isLoggedIn()
34+
} else {
35+
next()
36+
}
37+
})
38+
```

docs/api/go.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `router.go(path)`
2+
3+
Programatically navigate to a new route.
4+
5+
### Arguments
6+
7+
- `path: String`
8+
9+
The path must be a plain path (i.e. no dynamic segments or star segments). The the path doesn't start with `/`, it will be resolved relative to the current active path.

docs/api/map.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# `router.map(routeMap)`
2+
3+
The main method to define route mappings for the router.
4+
5+
### Arguments
6+
7+
- `routeMap: Object`
8+
9+
An object whose keys are paths and values are route config objects. For path matching rules, see [Route Matching](../route.html#route-matching).
10+
11+
### Route Config Object
12+
13+
A route config object can contain two fields:
14+
15+
- `component`: The Vue component to render into the top-level `<router-view>` outlet when this path is matched. The value could either be a constructor returned by calling `Vue.extend`, or a plain component options object. In the latter case the router will implicitly call `Vue.extend` for you.
16+
17+
- `subRoutes`: You can nest another sub route-map here. For each sub path in the `routeRoutes` map, the router will match it against the full path by appending it to the parent path. The matched component will be rendered into the parent route component's `<router-view>` outlet.
18+
19+
### Example
20+
21+
``` js
22+
router.map({
23+
// component constructor
24+
'/a': {
25+
component: Vue.extend({ /* ... */ })
26+
},
27+
// plain component options object
28+
'/b': {
29+
component: {
30+
template: '<p>Hello from /b</p>'
31+
}
32+
},
33+
// nested routes
34+
'/c': {
35+
component: {
36+
// simply render the child view
37+
template: '<router-view></router-view>'
38+
},
39+
subRoutes: {
40+
// rendered when the path is /c/d
41+
'/d': { component: { template: 'D' }},
42+
// rendered when the path is /c/e
43+
'/e': { component: { template: 'E' }}
44+
}
45+
}
46+
})
47+
```

docs/api/on.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `router.on(path, config)`
2+
3+
Add a single root-level route configuration. Internally, `router.map()` simply calls `router.on()` for each key-value pair in the router map object it receives.
4+
5+
### Arguments
6+
7+
- `path: String` - see [Route Matching](../route.md#route-matching)
8+
- `config: Object` - see [Route Config Object](map.html#route-config-object).
9+
10+
### Example
11+
12+
``` js
13+
router.on('/user/:userId', {
14+
component: {
15+
template: '<div>{{$route.params.userId}}</div>'
16+
}
17+
})
18+
```

docs/api/properties.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Router Instance Properties
2+
3+
> Only public properties are documented here.
4+
5+
### `router.app`
6+
7+
- type: `Vue`
8+
9+
The root Vue instance managed by the router instance. This instance is created from the Vue component contructor you passed into `router.start()`.
10+
11+
### `router.mode`
12+
13+
- type: `String`
14+
15+
One of `html5`, `hash` or `abstract`.
16+
17+
- **`html5`**: uses HTML5 history API and listens to `popstate` events. Supports [`saveScrollPosition`](../options.html#savescrollposition).
18+
19+
- **`hash`**: uses `location.hash` and listens to `hashchange` events. When you specify `history: true` when creating the router, it will fallback into hash mode in browsers that do not support the history API.
20+
21+
- **`abstract`**: does not listen to any events. Will auto-fallback into this mode if `window` is not present.

docs/api/redirect.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `router.redirect(redirectMap)`
2+
3+
Configures global redirection rules for the router. Global redirections are performed before matching the current path. If a redirection is found, the originally visited path will simply be skipped and will not leave a record in the history.
4+
5+
### Arguments
6+
7+
- `redirectMap: Object`
8+
9+
The redirect map object should be in the form of `{ fromPath: toPath, ... }`. The paths can contain dynamic segments.
10+
11+
### Example
12+
13+
``` js
14+
router.rediect({
15+
16+
// redirect any navigation to /a to /b
17+
'/a': '/b',
18+
19+
// redirect can contian dynamic segments
20+
// the dynamic segment names must match
21+
'/user/:userId': '/profile/:userId'
22+
})
23+
```

0 commit comments

Comments
 (0)