Skip to content

Commit f098345

Browse files
committed
Merge branch 'next-doc' into next
2 parents fbb0617 + 7da090a commit f098345

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+8207
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ selenium-debug.log
77
dist/*.gz
88
dist/*.map
99
explorations
10+
docs/_book

build/update-docs.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cd docs
2+
rm -rf _book
3+
gitbook build
4+
cp assets/circle.yml _book/circle.yml
5+
cp assets/CNAME _book/CNAME
6+
cd _book
7+
git init
8+
git add -A
9+
git commit -m 'update book'
10+
git push -f [email protected]:vuejs/vue-router.git master:gh-pages

docs/LANGS.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [English](en/)

docs/assets/CNAME

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rc.router.vuejs.org

docs/assets/circle.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
general:
2+
branches:
3+
ignore:
4+
- gh-pages

docs/assets/illustrations.ai

+6,381
Large diffs are not rendered by default.

docs/book.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"gitbook": "2.x.x",
3+
"plugins": ["edit-link", "github"],
4+
"pluginsConfig": {
5+
"edit-link": {
6+
"base": "https://github.com/vuejs/vue-router/tree/master/docs",
7+
"label": "Edit This Page"
8+
},
9+
"github": {
10+
"url": "https://github.com/vuejs/vue-router/"
11+
}
12+
},
13+
"links": {
14+
"sharing": {
15+
"facebook": false,
16+
"twitter": false
17+
}
18+
}
19+
}

docs/en/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# vue-router documentation [![npm package](https://img.shields.io/npm/v/vue-router.svg)](https://www.npmjs.com/package/vue-router)
2+
3+
**[Release Notes](https://github.com/vuejs/vue-router/releases)**
4+
5+
- [Installation](installation.md)
6+
- [Getting Started](getting-started/README.md)
7+
- [Basic Usage](getting-started/basic.md)
8+
- [Route Object & Route Matching](getting-started/route.md)
9+
- [Named Routes](getting-started/named-routes.md)
10+
- [Programmatic Navigation](getting-started/navigation.md)
11+
- [router-link](getting-started/link.md)
12+
- [router-view](getting-started/view.md)
13+
- [Advanced Routing](advanced-routing/README.md)
14+
- [Nested Routes](advanced-routing/nested.md)
15+
- [Named Views](advanced-routing/named-views.md)
16+
- [Lazy Loading Routes](advanced-routing/lazy.md)
17+
- [Async Routes](advanced-routing/async-routes.md)
18+
- [Navigation](navigation/README.md)
19+
- [Router Hooks](navigation/router-hooks.md)
20+
- [Routes Hooks](navigation/routes-hooks.md)
21+
- [Views Transitions](navigation/views-transitions.md)
22+
- [API](api/README.md)
23+
- [VueRouter](api/router/README.md)
24+
- [constructor](api/router/constructor.md)
25+
- [currentRoute](api/router/current-route.md)
26+
- [beforeEach](api/router/before-each.md)
27+
- [afterEach](api/router/after-each.md)
28+
- [push](api/router/push.md)
29+
- [replace](api/router/replace.md)
30+
- [go](api/router/go.md)
31+
- [back](api/router/back.md)
32+
- [forward](api/router/back.md)
33+
- [getMatchedComponents](api/router/get-matched-components.md)
34+
- [$route](api/route.md)
35+
- [router-link](api/router-link.md)
36+
- [router-view](api/router-view.md)
37+
- [beforeRouteEnter](api/beforeRouteEnter.md)
38+
- [beforeRouteLeave](api/beforeRouteLeave.md)
39+
- [beforeEnter](api/beforeEnter.md)
40+
- [Types](api/types/README.md)
41+
- [RouterOptions](api/types/router-options.md)
42+
- [RedirectOption](api/types/redirect-option.md)
43+
- [RouteConfig](api/types/route-config.md)
44+
- [RouteRecord](api/types/route-record.md)
45+
- [RouteMap](api/types/route-map.md)
46+
- [StringHash](api/types/string-hash.md)
47+
- [Location](api/types/location.md)
48+
- [RawLocation](api/types/raw-location.md)
49+
- [Route](api/types/route.md)
50+
- [Matcher](api/types/matcher.md)

docs/en/SUMMARY.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- [Installation](installation.md)
2+
- Getting Started
3+
- [Basic Usage](getting-started/basic.md)
4+
- [Route Object & Route Matching](getting-started/route.md)
5+
- [Named Routes](getting-started/named-routes.md)
6+
- [Programmatic Navigation](getting-started/navigation.md)
7+
- [<router-link>](getting-started/link.md)
8+
- [<router-view>](getting-started/view.md)
9+
- Advanced Routing
10+
- [Nested Routes](advanced-routing/nested.md)
11+
- [Named Views](advanced-routing/named-views.md)
12+
- [Lazy Loading Routes](advanced-routing/lazy.md)
13+
- [Async Routes](advanced-routing/async-routes.md)
14+
- Navigation
15+
- [Router Hooks](navigation/router-hooks.md)
16+
- [Routes Hooks](navigation/routes-hooks.md)
17+
- [Views Transitions](navigation/views-transitions.md)
18+
- [API](api/README.md)
19+
- [VueRouter](api/router/README.md)
20+
- [constructor](api/router/constructor.md)
21+
- [currentRoute](api/router/current-route.md)
22+
- [beforeEach](api/router/before-each.md)
23+
- [afterEach](api/router/after-each.md)
24+
- [push](api/router/push.md)
25+
- [replace](api/router/replace.md)
26+
- [go](api/router/go.md)
27+
- [back](api/router/back.md)
28+
- [forward](api/router/back.md)
29+
- [getMatchedComponents](api/router/get-matched-components.md)
30+
- [$route](api/route.md)
31+
- [router-link](api/router-link.md)
32+
- [router-view](api/router-view.md)
33+
- [beforeRouteEnter](api/beforeRouteEnter.md)
34+
- [beforeRouteLeave](api/beforeRouteLeave.md)
35+
- [beforeEnter](api/beforeEnter.md)
36+
- [Types](api/types/README.md)
37+
- [RouterOptions](api/types/router-options.md)
38+
- [RedirectOption](api/types/redirect-option.md)
39+
- [RouteConfig](api/types/route-config.md)
40+
- [RouteRecord](api/types/route-record.md)
41+
- [RouteMap](api/types/route-map.md)
42+
- [StringHash](api/types/string-hash.md)
43+
- [Location](api/types/location.md)
44+
- [RawLocation](api/types/raw-location.md)
45+
- [Route](api/types/route.md)
46+
- [Matcher](api/types/matcher.md)
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Async Routes
2+
3+
Sometimes you need to fetch data from the server before rendering a route. For
4+
example, before visiting a user profile, you have to fetch his data from the
5+
server. We can achieve this in two different ways. Each way provide a different
6+
UX approach and both of them are valid designs.
7+
8+
9+
## Fetching before navigation
10+
11+
This method consists on fetching the data before actually navigating to the new
12+
route. For those using vue-router 1, this works as the `activate` hook. In
13+
vue-router 2 you use the `beforeRouteEnter` hook to control the navigation flow.
14+
You get access to the `route`, a `redirect` function and a `next` callback to
15+
let the navigation end. Not calling the `next` callback will simply cancel the
16+
navigation. Note that this hook is
17+
18+
``` js
19+
export default {
20+
data () {
21+
return {
22+
loading: false,
23+
post: null,
24+
error: null
25+
}
26+
},
27+
beforeRouteEnter (route, redirect, next) {
28+
// fetch the data when the view is created and the data is
29+
// already being observed
30+
this.fetchData(route.params.id, next)
31+
},
32+
watch: {
33+
// call again the method if the route changes
34+
$route () {
35+
this.fetchData(this.$route.params.id)
36+
}
37+
},
38+
methods: {
39+
fetchData (id, cb) {
40+
this.error = this.post = null
41+
this.loading = true
42+
getPost(id, (err, post) => {
43+
this.loading = false
44+
if (err) {
45+
this.error = err.toString()
46+
} else {
47+
this.post = post
48+
}
49+
cb && cb()
50+
})
51+
}
52+
}
53+
}
54+
```
55+
56+
The user still can use the application while the resource is being fetched. It
57+
is therefore recommended to show him with a progress bar or any other indicator
58+
that the web site is waiting for data. If, during this time, the user navigates
59+
somewhere else by clicking on a link, the navigation waiting for data will never
60+
take place.
61+
62+
## Fetching inside the view
63+
64+
This method consists on fetching data during the component lifecycle. It allows
65+
you to define how the content of your view is presented while the resources are
66+
loading. Using this approach let you handle how loading in handled differently
67+
for each view.
68+
69+
Replacing `beforeRouteEnter` with `created` is almost all you need to do to
70+
switch to the other fetching method.
71+
72+
``` js
73+
export default {
74+
data () {
75+
return {
76+
loading: false,
77+
post: null,
78+
error: null
79+
}
80+
},
81+
created () {
82+
// fetch the data when the view is created and the data is
83+
// already being observed
84+
this.fetchData()
85+
},
86+
watch: {
87+
// call again the method if the route changes
88+
'$route': 'fetchData'
89+
},
90+
methods: {
91+
fetchData () {
92+
this.error = this.post = null
93+
this.loading = true
94+
getPost(this.$route.params.id, (err, post) => {
95+
this.loading = false
96+
if (err) {
97+
this.error = err.toString()
98+
} else {
99+
this.post = post
100+
}
101+
})
102+
}
103+
}
104+
}
105+
```
106+
107+
The component lifecycle will go on and once the data is fetched, the view will update acordingly. This way you can choose what to show while the data is being fetched. Here we're just displaying _Loading..._:
108+
109+
``` html
110+
<template>
111+
<div class="post">
112+
<div class="loading" v-if="loading">Loading...</div>
113+
<div v-if="error" class="error">
114+
{{ error }}
115+
</div>
116+
<transition name="slide">
117+
<!--
118+
giving the post container a unique key triggers transitions
119+
when the post id changes.
120+
-->
121+
<div v-if="post" class="content" :key="post.id">
122+
<h2>{{ post.title }}</h2>
123+
<p>{{ post.body }}</p>
124+
</div>
125+
</transition>
126+
</div>
127+
</template>
128+
```

docs/en/advanced-routing/lazy.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Lazy Loading Routes
2+
3+
When you are using bundlers like Webpack or Browserify, it's trivially easy to
4+
lazy-load a route component using Vue.js' built-in
5+
[async component functionality](http://vuejs.org/guide/components.html#Async-Components).
6+
Instead of directly defining your route component, you define it as a function
7+
that will asynchronously resolve the actual component definition:
8+
9+
``` js
10+
const router = new VueRouter({
11+
routes: [
12+
{
13+
path: '/async',
14+
component: function (resolve) {
15+
// somehow retrieve your component definition from server...
16+
resolve(MyComponent)
17+
}
18+
}
19+
]
20+
})
21+
```
22+
23+
Now, manually handling component retrieval is less than ideal, but bundlers like
24+
Webpack & Browserify both provides ways to make it easier.
25+
26+
### Webpack
27+
28+
Webpack has built-in support for async code-splitting. You can use the AMD-like
29+
`require` syntax in your code to indicate an async code-split point:
30+
31+
``` js
32+
require(['./MyComponent.vue'], function (MyComponent) {
33+
// code here runs after MyComponent.vue is asynchronously loaded.
34+
})
35+
```
36+
37+
Combined with the router it can simply look like this:
38+
39+
``` js
40+
const router = new VueRouter({
41+
routes: [
42+
{
43+
path: '/async',
44+
component: function (resolve) {
45+
require(['./MyComponent.vue'], resolve)
46+
}
47+
}
48+
]
49+
})
50+
```
51+
52+
Now, `MyComponent.vue`, along with any dependencies that are only used by itself,
53+
will be loaded asynchronously only when the route `/async` needs to be rendered.
54+
55+
### Browserify
56+
57+
TODO make this part as easy as wepack's
58+
59+
It's a bit more tricky to achieve the same with Browserify, but it's possible
60+
with the
61+
[`partition-bundle` plugin](https://github.com/substack/browserify-handbook/blob/master/readme.markdown#partition-bundle).
62+
You will have to manually declare your bundle mappings in a `json` file:
63+
64+
``` json
65+
{
66+
"main.js": ["./main.js"],
67+
"my-component.js": ["./MyComponent.vue"]
68+
}
69+
```
70+
71+
Then in `main.js` you would do something similar, using the `loadjs` function instead of `require`:
72+
73+
``` js
74+
const router = new VueRouter({
75+
routes: [
76+
{
77+
path: '/async',
78+
component: function (resolve) {
79+
loadjs(['./MyComponent.vue'], resolve)
80+
}
81+
}
82+
]
83+
})
84+
```
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Named Views
2+
3+
Sometimes you need to show multiples views at the same time instead of nesting
4+
them. This is where named views came in handy. Instead of having one single
5+
outlet in your view, you can have multiple and give each of them a name. A
6+
`router-view` without a name will be given `default` as its name.
7+
8+
``` html
9+
<router-view class="view one"></router-view>
10+
<router-view class="view two" name="a"></router-view>
11+
<router-view class="view three" name="b"></router-view>
12+
```
13+
14+
A view is rendered by using a component, therefore multiple views require
15+
multiple components for the same route. Make sure to use the `components` (with
16+
an s) option:
17+
18+
``` js
19+
const router = new VueRouter({
20+
routes: [
21+
{
22+
path: '/',
23+
components: {
24+
default: Foo,
25+
a: Bar,
26+
b: Baz
27+
}
28+
}
29+
]
30+
})
31+
```
32+
33+
A working demo of this example can be found
34+
[here](https://jsfiddle.net/posva/9b80nxx1/).

0 commit comments

Comments
 (0)