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/advanced/navigation-guards.md
+18-12Lines changed: 18 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ You can register global before guards using `router.beforeEach`:
9
9
```js
10
10
constrouter=newVueRouter({ ... })
11
11
12
-
router.beforeEach((route, redirect, next) => {
12
+
router.beforeEach((to, from, next) => {
13
13
// ...
14
14
})
15
15
```
@@ -18,18 +18,24 @@ Global before guards are called in creation order, whenever a navigation is trig
18
18
19
19
Every guard function receives three arguments:
20
20
21
-
-`route: Route`: the target [Route Object](../api/route-object.md) being navigated to.
21
+
-**`to: Route`**: the target [Route Object](../api/route-object.md) being navigated to.
22
22
23
-
-`redirect: Function`: calling this function will abort the current navigation and start a new navigation towards the redirect target.
23
+
-**`from: Route`**: the current route being navigated away from.
24
24
25
-
-`next: Function`: resolve this guard and proceed to the next guard in the pipeline. If there are no hooks left, then the navigation is **confirmed**.
25
+
-**`next: Function`**: this function must be called to **resolve**the hook. The action depends on the arguments provided to `next`:
26
26
27
-
**If neither `redirect` nor `next` is called, the navigation will be cancelled.**
27
+
-**`next()`**: move on to the next hook in the pipeline. If no hooks are left, the navigation is **confirmed**.
28
28
29
-
You can also register global after hooks, however unlike guards, these hooks are much simpler and cannot affect the navigation:
29
+
-**`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.
30
+
31
+
-**`next('/')` or `next({ path: '/' })`**: redirect to a different location. The current navigation will be aborted and a new one will be started.
32
+
33
+
**Make sure to always call the `next` function, otherwise the hook will never be resolved.**
34
+
35
+
You can also register global after hooks, however unlike guards, these hooks do not get a `next` function and cannot affect the navigation:
30
36
31
37
```js
32
-
router.afterEach(route=> {
38
+
router.afterEach((to, from)=> {
33
39
// ...
34
40
})
35
41
```
@@ -44,7 +50,7 @@ const router = new VueRouter({
44
50
{
45
51
path:'/foo',
46
52
component: Foo,
47
-
beforeEnter: (route, redirect, next) => {
53
+
beforeEnter: (to, from, next) => {
48
54
// ...
49
55
}
50
56
}
@@ -61,12 +67,12 @@ Finally, you can directly define route navigation guards inside route components
61
67
```js
62
68
constFoo= {
63
69
template:`...`,
64
-
beforeRouteEnter (route, redirect, next) {
70
+
beforeRouteEnter (to, from, next) {
65
71
// called before the route that renders this component is confirmed.
66
72
// does NOT have access to `this` component instance,
67
73
// because it has not been created yet when this guard is called!
68
74
},
69
-
beforeRouteLeave (route, redirect, next) {
75
+
beforeRouteLeave (to, from, next) {
70
76
// called when the route that renders this component is about to
71
77
// be navigated away from.
72
78
// has access to `this` component instance.
@@ -79,11 +85,11 @@ The `beforeRouteEnter` guard does **NOT** have access to `this`, because the gua
79
85
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:
80
86
81
87
```js
82
-
beforeRouteEnter (route, redirect, next) {
88
+
beforeRouteEnter (to, from, next) {
83
89
next(vm=> {
84
90
// access to component instance via `vm`
85
91
})
86
92
}
87
93
```
88
94
89
-
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 simply not calling `next` or `redirect`.
95
+
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)`.
0 commit comments