|
| 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