|
| 1 | +# Navigation Guards (En) <br><br> *Cette page est en cours de traduction française. Revenez une autre fois pour lire une traduction achevée ou [participez à la traduction française ici](https://github.com/vuejs-fr/vue-router).* |
| 2 | + |
| 3 | +As the name suggests, the navigation guards provided by `vue-router` are primarily used to guard navigations either by redirecting it or canceling it. There are a number of ways to hook into the route navigation process: globally, per-route, or in-component. |
| 4 | + |
| 5 | +Remember that **params or query changes won't trigger enter/leave navigation guards**. You can either [watch the `$route` object](../essentials/dynamic-matching.md#reacting-to-params-changes) to react to those changes, or use the `beforeRouteUpdate` in-component guard. |
| 6 | + |
| 7 | +### Global Guards |
| 8 | + |
| 9 | +You can register global before guards using `router.beforeEach`: |
| 10 | + |
| 11 | +``` js |
| 12 | +const router = new VueRouter({ ... }) |
| 13 | + |
| 14 | +router.beforeEach((to, from, next) => { |
| 15 | + // ... |
| 16 | +}) |
| 17 | +``` |
| 18 | + |
| 19 | +Global before guards are called in creation order, whenever a navigation is triggered. Guards may be resolved asynchronously, and the navigation is considered **pending** before all hooks have been resolved. |
| 20 | + |
| 21 | +Every guard function receives three arguments: |
| 22 | + |
| 23 | +- **`to: Route`**: the target [Route Object](../api/route-object.md) being navigated to. |
| 24 | + |
| 25 | +- **`from: Route`**: the current route being navigated away from. |
| 26 | + |
| 27 | +- **`next: Function`**: this function must be called to **resolve** the hook. The action depends on the arguments provided to `next`: |
| 28 | + |
| 29 | + - **`next()`**: move on to the next hook in the pipeline. If no hooks are left, the navigation is **confirmed**. |
| 30 | + |
| 31 | + - **`next(false)`**: abort the current navigation. If the browser URL was changed (either manually by the user or via back button), it will be reset to that of the `from` route. |
| 32 | + |
| 33 | + - **`next('/')` or `next({ path: '/' })`**: redirect to a different location. The current navigation will be aborted and a new one will be started. |
| 34 | + |
| 35 | + - **`next(error)`**: (2.4.0+) if the argument passed to `next` is an instance of `Error`, the navigation will be aborted and the error will be passed to callbacks registered via `router.onError()`. |
| 36 | + |
| 37 | +**Make sure to always call the `next` function, otherwise the hook will never be resolved.** |
| 38 | + |
| 39 | +### Global Resolve Guards |
| 40 | + |
| 41 | +> New in 2.5.0 |
| 42 | +
|
| 43 | +In 2.5.0+ you can register a global guard with `router.beforeResolve`. This is similar to `router.beforeEach`, with the difference that resolve guards will be called right before the navigation is confirmed, **after all in-component guards and async route components are resolved**. |
| 44 | + |
| 45 | +### Global After Hooks |
| 46 | + |
| 47 | +You can also register global after hooks, however unlike guards, these hooks do not get a `next` function and cannot affect the navigation: |
| 48 | + |
| 49 | +``` js |
| 50 | +router.afterEach((to, from) => { |
| 51 | + // ... |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +### Per-Route Guard |
| 56 | + |
| 57 | +You can define `beforeEnter` guards directly on a route's configuration object: |
| 58 | + |
| 59 | +``` js |
| 60 | +const router = new VueRouter({ |
| 61 | + routes: [ |
| 62 | + { |
| 63 | + path: '/foo', |
| 64 | + component: Foo, |
| 65 | + beforeEnter: (to, from, next) => { |
| 66 | + // ... |
| 67 | + } |
| 68 | + } |
| 69 | + ] |
| 70 | +}) |
| 71 | +``` |
| 72 | + |
| 73 | +These guards have the exact same signature as global before guards. |
| 74 | + |
| 75 | +### Sécurisation intra-composants |
| 76 | + |
| 77 | +Finally, you can directly define route navigation guards inside route components (the ones passed to the router configuration) with the following options: |
| 78 | + |
| 79 | +- `beforeRouteEnter` |
| 80 | +- `beforeRouteUpdate` (added in 2.2) |
| 81 | +- `beforeRouteLeave` |
| 82 | + |
| 83 | +``` js |
| 84 | +const Foo = { |
| 85 | + template: `...`, |
| 86 | + beforeRouteEnter (to, from, next) { |
| 87 | + // called before the route that renders this component is confirmed. |
| 88 | + // does NOT have access to `this` component instance, |
| 89 | + // because it has not been created yet when this guard is called! |
| 90 | + }, |
| 91 | + beforeRouteUpdate (to, from, next) { |
| 92 | + // called when the route that renders this component has changed, |
| 93 | + // but this component is reused in the new route. |
| 94 | + // For example, for a route with dynamic params /foo/:id, when we |
| 95 | + // navigate between /foo/1 and /foo/2, the same Foo component instance |
| 96 | + // will be reused, and this hook will be called when that happens. |
| 97 | + // has access to `this` component instance. |
| 98 | + }, |
| 99 | + beforeRouteLeave (to, from, next) { |
| 100 | + // called when the route that renders this component is about to |
| 101 | + // be navigated away from. |
| 102 | + // has access to `this` component instance. |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +The `beforeRouteEnter` guard does **NOT** have access to `this`, because the guard is called before the navigation is confirmed, thus the new entering component has not even been created yet. |
| 108 | + |
| 109 | +However, you can access the instance by passing a callback to `next`. The callback will be called when the navigation is confirmed, and the component instance will be passed to the callback as the argument: |
| 110 | + |
| 111 | +``` js |
| 112 | +beforeRouteEnter (to, from, next) { |
| 113 | + next(vm => { |
| 114 | + // access to component instance via `vm` |
| 115 | + }) |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +You can directly access `this` inside `beforeRouteLeave`. The leave guard is usually used to prevent the user from accidentally leaving the route with unsaved edits. The navigation can be canceled by calling `next(false)`. |
| 120 | + |
| 121 | +### The Full Navigation Resolution Flow |
| 122 | + |
| 123 | +1. Navigation triggered |
| 124 | +2. Call leave guards in deactivated components |
| 125 | +3. Call global `beforeEach` guards |
| 126 | +4. Call `beforeRouteUpdate` guards in reused components (2.2+) |
| 127 | +5. Call `beforeEnter` in route configs |
| 128 | +6. Resolve async route components |
| 129 | +7. Call `beforeRouteEnter` in activated components |
| 130 | +8. Call global `beforeResolve` guards (2.5+) |
| 131 | +9. Navigation confirmed. |
| 132 | +10. Call global `afterEach` hooks. |
| 133 | +11. DOM updates triggered. |
| 134 | +12. Call callbacks passed to `next` in `beforeRouteEnter` guards with instantiated instances. |
0 commit comments