Skip to content

Commit 29f833e

Browse files
committed
update docs for 0.7.2
1 parent e29de0a commit 29f833e

File tree

2 files changed

+69
-20
lines changed

2 files changed

+69
-20
lines changed

docs/en/pipeline/hooks.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ You can implement these hooks under your component's `route` option:
1515
Vue.component('hook-example', {
1616
// ... other options
1717
route: {
18-
activate: function (transition) {
18+
activate: function () {
1919
console.log('hook-example activated!')
20-
transition.next()
2120
},
22-
deactivate: function (transition) {
23-
console.log('hook-example deactivated!')
24-
transition.next()
21+
canDeactivate: function (transition) {
22+
console.log('You are not allowed to leave.')
23+
transition.abort()
2524
}
2625
}
2726
})
@@ -51,21 +50,46 @@ Each transition hook will receive a `transition` object as the only argument. Th
5150

5251
Cancel current transition and redirect to a different target route instead.
5352

54-
All transition hooks are considered asynchronous by default. In order to signal the transition to progress, you have three options:
53+
### Hook Resolution Rules
54+
55+
We often need to perform asynchronous tasks inside transition hooks. The transition will not proceed until an asynchronous hook has been resolved. Here are the rules to determine when a hook should be considered resolved:
56+
57+
1. If the hook returns a Promise, the hook will be resolved when the Promise is resolved. [See details below](#returning-promise-in-hooks).
58+
59+
2. If the hook doesn't return a Promise, and doesn't expect any argument, it is resolved synchronously. For example:
60+
61+
``` js
62+
route: {
63+
activate: function (/* no argument here */) {
64+
// will resolve synchronously as long as not returning a Promise
65+
}
66+
}
67+
```
5568

56-
1. Explicitly call one of `next`, `abort` or `redirect`.
69+
3. If the hook doesn't return a Promise, but expects an argument (`transition`), then the hook will only be resolved when one of `transition.next()`, `transition.abort()` or `transition.redirect()` is called. For example:
5770

58-
2. Return a Promise. Details below.
71+
``` js
72+
route: {
73+
activate: function (transition) {
74+
// resolve after 1 second
75+
setTimeout(transition.next, 1000)
76+
}
77+
}
78+
```
5979

60-
3. For validation hooks (`canActivate` and `canDeactivate`), you can synchronously return a Boolean value.
80+
4. In validation hooks such as `canActivate`, `canDeactivate` and the [global beforeEach hook](../api/before-each.md), returning a Boolean will synchronously resolve the hook, even if the hooks has the `transition` argument.
6181

6282
### Returning Promise in Hooks
6383

64-
When you return a Promise in a transition hook, `transition.next` will be called for you when the Promise 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`.
84+
- When you return a Promise in a transition hook, `transition.next` will be called for you when the Promise resolves successfully.
85+
86+
- If the Promise is rejected during validation phase, it will call `transition.abort`.
87+
88+
- If the Promise is rejected during activation phase, it will call `transition.next`.
6589

66-
For validation hooks (`canActivate` and `canDeactivate`), if the Promise's resolved value is falsy, it will also abort the transition.
90+
- For validation hooks, if the Promise's resolved value is falsy, it will also abort the transition.
6791

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.
92+
- If a rejected promise has an uncaught error, it will be thrown unless you suppress it with the `suppressTransitionError` option when creating the router.
6993

7094
**Example:**
7195

docs/zh-cn/pipeline/hooks.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,46 @@ Vue.component('hook-example', {
5151

5252
取消当前切换并重定向到另一个路由。
5353

54-
所有的切换钩子默认都被当作异步的。想要控制切换的进度, 你有三个选择:
54+
### 钩子函数异步 resolve 规则
5555

56-
1. 显示的调用 `next`, `abort` 或者 `redirect`
56+
我们经常需要在钩子函数中进行异步操作。在一个异步的钩子被 resolve 之前,切换会处于暂停状态。钩子的 resolve 遵循以下规则:
5757

58-
2. 返回一个 Promise ,详情见下文。
58+
1. 如果钩子返回一个 Promise,则钩子何时 resolve 取决于该 Promise 何时 resolve。[更多细节](#%E5%9C%A8%E9%92%A9%E5%AD%90%E4%B8%AD%E8%BF%94%E5%9B%9E-promise)
5959

60-
3. 对于验证的钩子( `canActivate``canDeactivate` ),你可以同步的返回一个 Boolean 值。
60+
2. 如果钩子既不返回 Promise,也没有任何参数,则该钩子将被同步 resolve。例如:
6161

62-
### 在钩子中返回Promise
62+
``` js
63+
route: {
64+
activate: function (/* 没有参数 */) {
65+
// 如果不返回 Promise,则同步 resolve
66+
}
67+
}
68+
```
69+
70+
3. 如果钩子不返回 Promise,但是有一个参数 (`transition`),则钩子会等到 `transition.next()`, `transition.abort()` 或是 `transition.redirect()` 之一被调用才 resolve。例如:
71+
72+
``` js
73+
route: {
74+
activate: function (transition) {
75+
// 一秒后 resolve
76+
setTimeout(transition.next, 1000)
77+
}
78+
}
79+
```
80+
81+
4. 在验证类的钩子,比如 `canActivate`, `canDeactivate` 以及[全局 beforeEach 钩子](../api/before-each.md) 中,如果返回值是一个布尔值 (Boolean),也会使得钩子同步 resolve。
82+
83+
### 在钩子中返回 Promise
84+
85+
- 当在钩子函数中返回一个 Promise 时,系统会在该 Promise 被 resolve 之后自动调用`transition.next`
86+
87+
- 如果 Promise 在验证阶段被 reject,系统会调用 `transition.abort`
6388

64-
当在钩子函数中返回一个 Promise 时,系统会在 Promise 断定( resolve )之后自动调用`transition.next`如果 Promise 在验证阶段被拒绝( rejected ),系统会调用 `transition.abort` ;如果是在激活阶段被调用,系统会调用 `transition.next`
89+
- 如果 Promise 在激活阶段被 resolve,系统会调用 `transition.next`
6590

66-
对于激活阶段的钩子`canActivate``canDeactivate` ),如果 Promise 断定( resolved )之后的值是假值( falsy value ),系统会中断此次切换。
91+
- 对于验证类钩子`canActivate``canDeactivate` ),如果 Promise resolve 之后的值是假值( falsy value ),系统会中断此次切换。
6792

68-
如果一个被拒绝( rejected )的 Promise 抛出了未捕获的异常,这个异常会继续向上抛出,除非在创建路由器的时候启用了参数 `suppressTransitionError`
93+
- 如果一个被 reject 的 Promise 抛出了未捕获的异常,这个异常会继续向上抛出,除非在创建路由器的时候启用了参数 `suppressTransitionError`
6994

7095
**例子:**
7196

0 commit comments

Comments
 (0)