Skip to content

Commit c4f9836

Browse files
committed
update docs for new guard signature
1 parent 1a068b3 commit c4f9836

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

docs/en/advanced/data-fetching.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ export default {
8282
error: null
8383
}
8484
},
85-
beforeRouteEnter (route, redirect, next) {
86-
getPost(route.params.id, (err, post) => {
85+
beforeRouteEnter (to, from, next) {
86+
getPost(to.params.id, (err, post) => {
8787
if (err) {
8888
// display some global error message
89+
next(false)
8990
} else {
9091
next(vm => {
9192
vm.post = post

docs/en/advanced/meta.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ All route records matched by a route are exposed on the `$route` object (and als
3232
An example use case is checking for a meta field in the global navigation guard:
3333

3434
``` js
35-
router.beforeEach((route, redirect, next) => {
36-
if (route.matched.some(record => record.meta.requiresAuth)) {
35+
router.beforeEach((to, from, next) => {
36+
if (to.matched.some(record => record.meta.requiresAuth)) {
3737
// this route requires auth, check if logged in
3838
// if not, redirect to login page.
3939
if (!auth.loggedIn()) {
40-
redirect({
40+
next({
4141
path: '/login',
42-
query: { redirect: route.fullPath }
42+
query: { redirect: to.fullPath }
4343
})
4444
} else {
4545
next()
4646
}
4747
} else {
48-
next()
48+
next() // make sure to always call next()!
4949
}
5050
})
5151
```

docs/en/advanced/navigation-guards.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You can register global before guards using `router.beforeEach`:
99
``` js
1010
const router = new VueRouter({ ... })
1111

12-
router.beforeEach((route, redirect, next) => {
12+
router.beforeEach((to, from, next) => {
1313
// ...
1414
})
1515
```
@@ -18,18 +18,24 @@ Global before guards are called in creation order, whenever a navigation is trig
1818

1919
Every guard function receives three arguments:
2020

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

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

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`:
2626

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**.
2828

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

3137
``` js
32-
router.afterEach(route => {
38+
router.afterEach((to, from) => {
3339
// ...
3440
})
3541
```
@@ -44,7 +50,7 @@ const router = new VueRouter({
4450
{
4551
path: '/foo',
4652
component: Foo,
47-
beforeEnter: (route, redirect, next) => {
53+
beforeEnter: (to, from, next) => {
4854
// ...
4955
}
5056
}
@@ -61,12 +67,12 @@ Finally, you can directly define route navigation guards inside route components
6167
``` js
6268
const Foo = {
6369
template: `...`,
64-
beforeRouteEnter (route, redirect, next) {
70+
beforeRouteEnter (to, from, next) {
6571
// called before the route that renders this component is confirmed.
6672
// does NOT have access to `this` component instance,
6773
// because it has not been created yet when this guard is called!
6874
},
69-
beforeRouteLeave (route, redirect, next) {
75+
beforeRouteLeave (to, from, next) {
7076
// called when the route that renders this component is about to
7177
// be navigated away from.
7278
// has access to `this` component instance.
@@ -79,11 +85,11 @@ The `beforeRouteEnter` guard does **NOT** have access to `this`, because the gua
7985
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:
8086

8187
``` js
82-
beforeRouteEnter (route, redirect, next) {
88+
beforeRouteEnter (to, from, next) {
8389
next(vm => {
8490
// access to component instance via `vm`
8591
})
8692
}
8793
```
8894

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

Comments
 (0)