Skip to content

Commit e014630

Browse files
committed
docs: add example for navigation guards
thanks to @lstar19
1 parent 5c5b8ab commit e014630

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

docs/guide/advanced/navigation-guards.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,24 @@ Every guard function receives three arguments:
3434

3535
- **`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()`](../../api/#router-onerror).
3636

37-
**Make sure to always call the `next` function exactly one time in each navigation guard, otherwise the hook will never be resolved or produce errors.**
37+
**Make sure that the `next` function is called exactly once in any given navigation guard. It can appear more than once, but only if the logical paths have no overlap, otherwise the hook will never be resolved or produce errors.** Here is an example of redirecting to user to `/login` if they are not authenticated:
38+
39+
```js
40+
// BAD
41+
router.beforeEach((to, from, next) => {
42+
if (!isAuthenticated) next('/login')
43+
// if the user is not authenticated, `next` is called twice
44+
next()
45+
})
46+
```
47+
48+
```js
49+
// GOOD
50+
router.beforeEach((to, from, next) => {
51+
if (!isAuthenticated) next('/login')
52+
else next()
53+
})
54+
```
3855

3956
## Global Resolve Guards
4057

0 commit comments

Comments
 (0)