Skip to content

Commit b7131b5

Browse files
committed
docs wip
1 parent 84a1ee1 commit b7131b5

File tree

9 files changed

+141
-25
lines changed

9 files changed

+141
-25
lines changed

docs/SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Table of Contents
1+
# vue-router documentation
22

33
- [Installation](installation.md)
44
- [Basic Usage](basic.md)
@@ -8,7 +8,7 @@
88
- [router-view](view.md)
99
- [v-link](link.md)
1010
- [Transition Pipeline](pipeline/README.md)
11-
- [Transition Object](pipeline/transition.md)
11+
- [Transition Hooks](pipeline/hooks.md)
1212
- [data](pipeline/data.md)
1313
- [activate](pipeline/activate.md)
1414
- [deactivate](pipeline/deactivate.md)

docs/basic.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Creating a Single-page Application with Vue.js + vue-router is dead simple. With
2020
### JavaScript
2121

2222
``` js
23-
// define some components
23+
// Define some components
2424
var Foo = Vue.extend({
2525
template: '<p>This is foo!</p>'
2626
})
@@ -29,19 +29,21 @@ var Bar = Vue.extend({
2929
template: '<p>This is bar!</p>'
3030
})
3131

32-
// the router needs a root component to render.
33-
// for demo purposes, we will just use an empty one
32+
// The router needs a root component to render.
33+
// For demo purposes, we will just use an empty one
3434
// because we are using the HTML as the app template.
3535
var App = Vue.extend({})
3636

37-
// create a router instance.
38-
// you can pass in additional options here, but let's
37+
// Create a router instance.
38+
// You can pass in additional options here, but let's
3939
// keep it simple for now.
4040
var router = new VueRouter()
4141

42-
// define some routes.
43-
// each route should map to a component.
44-
// we'll talk about nested routes later.
42+
// Define some routes.
43+
// Each route should map to a component. The "component" can
44+
// either be an actual component constructor created via
45+
// Vue.extend(), or just a component options object.
46+
// We'll talk about nested routes later.
4547
router.map({
4648
'/foo': {
4749
component: Foo
@@ -51,8 +53,8 @@ router.map({
5153
}
5254
})
5355

54-
// now we can start the app!
55-
// router will create an instance of App and mount to
56+
// Now we can start the app!
57+
// The router will create an instance of App and mount to
5658
// the element matching the selector #app.
5759
router.start(App, '#app')
5860
```

docs/pipeline/01.png

7.19 KB
Loading

docs/pipeline/02.png

9.8 KB
Loading

docs/pipeline/03.png

12.8 KB
Loading

docs/pipeline/04.png

18.4 KB
Loading

docs/pipeline/05.png

17.6 KB
Loading

docs/pipeline/README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
# Transition Pipeline
22

3-
Route components can configure its transition and data-loading behavior by implementing appropriate transition pipeline hooks. These hooks include:
3+
To better understand the pipeline of a route transition, let's imagine we have a router-enabled app, already rendered with three nested `<router-view>` with the path `/a/b/c`:
44

5-
- `data`
6-
- `activate`
7-
- `deactivate`
8-
- `canActivate`
9-
- `canDeactivate`
10-
- `canReuse`
5+
![](01.png)
116

12-
When a route transition is triggered, these hooks will be called in a specific order on affected view components.
7+
And then, the user navigates to a new path, `/a/d/e`, which requires us to update our rendered component tree to a new one:
138

14-
A route transition can be divided into three phases:
9+
![](02.png)
10+
11+
How would we go about that? There are a few things we need to do here:
12+
13+
1. We can potentially reuse component A, because it remains the same in the post-transition component tree.
14+
15+
2. We need to deactivate and remove component B and C.
16+
17+
3. We need to create and activate component D and E.
18+
19+
4. Before we actually perform step 2 & 3, we also want to make sure this transition is valid - that is, to make sure that all components involved in this transition **can** be deactivated/activated as desired.
20+
21+
### Transition Phases
22+
23+
With these in mind, we can divide a route transition pipeline into three phases:
1524

1625
1. **Reusability phase:**
1726

18-
Check if any component in the current view hierarchy can be reused in the new one.
27+
Check if any component in the current view hierarchy can be reused in the new one. This is done by comparing the two component trees, find out common components, and then check their reusability. By default, every component is reusable unless configured otherwise.
28+
29+
![reusability phase](03.png)
1930

2031
2. **Validation phase:**
2132

22-
Check if all current components can be deactivated, and if all new components can be activated.
33+
Check if all current components can be deactivated, and if all new components can be activated. This is by checking and calling their `canDeactivate` and `canActivate` route config hooks.
34+
35+
![validation phase](04.png)
36+
37+
Note the `canDeactivate` check bubbles bottom-up, while the `canActivate` check is top-down.
38+
39+
Any of these hooks can potentially abort the transition. If a transition is aborted during the validation phase, the router preserve current app state and restore the previous path.
2340

2441
3. **Activation phase:**
2542

26-
Deactivate current components and activate new components.
43+
Once all validation hooks have been called and none of them aborts the transition, the transition is now said to be valid. The router will now deactivate current components and activate new components.
44+
45+
![activation phase](05.png)
46+
47+
These hooks are called in the same order of the validation hooks, but their purpose is to give you the chance to do cleanup / preparation work before the visible component switching is executed. The interface will not update until all of the affected components' `deactivate` and `activate` hooks have resolved.
2748

28-
The diagram below demonstrates the full pipeline of a route transition:
49+
We will talk about transition hooks in detail next.

docs/pipeline/hooks.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Transition Hooks
2+
3+
A `<router-view>` component involved in a transition can control / react to the transition by implementing appropriate transition pipeline hooks. These hooks include:
4+
5+
- `data`
6+
- `activate`
7+
- `deactivate`
8+
- `canActivate`
9+
- `canDeactivate`
10+
- `canReuse`
11+
12+
You can implement these hooks under your component's `route` option:
13+
14+
``` js
15+
Vue.component('hook-example', {
16+
// ... other options
17+
route: {
18+
activate: function (transition) {
19+
console.log('hook-example activated!')
20+
transition.next()
21+
},
22+
deactivate: function (transition) {
23+
console.log('hook-example deactivated!')
24+
transition.next()
25+
}
26+
}
27+
})
28+
```
29+
30+
### The Transition Object
31+
32+
Each transition hook will receive a `transition` object as the only argument. The transition object exposes the following properties & methods:
33+
34+
- **transition.from**
35+
36+
A [route object](../route.html) representing the route we are transitioning from.
37+
38+
- **transition.to**
39+
40+
A route object representing the target path.
41+
42+
- **transition.next()**
43+
44+
Call this method to progress to the next step of the transition.
45+
46+
- **transition.abort([reason])**
47+
48+
Call this method to cancel / reject the transition.
49+
50+
- **transition.redirect(path)**
51+
52+
Cancel current transition and redirect to a different target route instead.
53+
54+
All transition hooks are considered asynchronous by default. In order to signal the transition to progress, you have three options:
55+
56+
1. Explicitly call one of `next`, `abort` or `redirect`.
57+
58+
2. Return a Promise. Details below.
59+
60+
3. For validation hooks (`canActivate` and `canDeactivate`), you can synchronously return a Boolean value.
61+
62+
### Returning Promise in Hooks
63+
64+
When you return a Promise in a transition hook, `transition.next` will be called for you when the Primise resolves. If the Promise is rejected during validation phase, it will call `transition.abort`; if it is rejected during activation phase, it will call `transition.next`.
65+
66+
For validation hooks (`canActivate` and `canDeactivate`), if the Promise's resolved value is falsy, it will also abort the transition.
67+
68+
If a rejected promise has an uncaught error, it will be thrown unless you suppress it with the `suppressTransitionError` option when creating the router.
69+
70+
**Example:**
71+
72+
``` js
73+
// inside component definition
74+
route: {
75+
canActivate: function () {
76+
// assuming the service returns a Promise that
77+
// resolve to either `true` or `false`.
78+
return authenticationService.isLoggedIn()
79+
},
80+
activate: function (transition) {
81+
return messageService
82+
.fetch(transition.to.params.messageId)
83+
.then((message) => {
84+
// set the data once it arrives.
85+
// the component will not display until this
86+
// is done.
87+
this.message = message
88+
})
89+
}
90+
}
91+
```
92+
93+
We are asynchronously fetching data in the `activate` hook here just for the sake of an example; Note that we also have the [`data` hook](data.html) which is in general more appropriate for this purpose.

0 commit comments

Comments
 (0)