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
Copy file name to clipboardExpand all lines: docs/en/pipeline/hooks.md
+36-12Lines changed: 36 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -15,13 +15,12 @@ You can implement these hooks under your component's `route` option:
15
15
Vue.component('hook-example', {
16
16
// ... other options
17
17
route: {
18
-
activate:function (transition) {
18
+
activate:function () {
19
19
console.log('hook-example activated!')
20
-
transition.next()
21
20
},
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()
25
24
}
26
25
}
27
26
})
@@ -51,21 +50,46 @@ Each transition hook will receive a `transition` object as the only argument. Th
51
50
52
51
Cancel current transition and redirect to a different target route instead.
53
52
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
+
```
55
68
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:
57
70
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
+
```
59
79
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.
61
81
62
82
### Returning Promise in Hooks
63
83
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`.
65
89
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.
67
91
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.
0 commit comments