Skip to content

Commit 1a91987

Browse files
committed
docs: navigation guards
1 parent df3bcc6 commit 1a91987

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

docs/en/SUMMARY.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
- [Installation](installation.md)
77
- Essentials
88
- [Getting Started](essentials/getting-started.md)
9-
- [Dynamic Route Matching](essentials/matching.md)
9+
- [Dynamic Route Matching](essentials/dynamic-matching.md)
1010
- [Nested Routes](essentials/nested-routes.md)
1111
- [Programmatic Navigation](essentials/navigation.md)
1212
- [Named Routes](essentials/named-routes.md)
1313
- [Named Views](essentials/named-views.md)
1414
- [Redirect and Alias](essentials/redirect-and-alias.md)
1515
- [Server Config for History Mode](essentials/server.md)
1616
- Advanced
17-
- [Navigation Guards](advanced/navigation-guards.md)
17+
- [Navigation Hooks](advanced/navigation-guards.md)
1818
- [Animations](advanced/animations.md)
1919
- [Data Fetching](advanced/data-fetching.md)
2020
- [Scroll Behavior](advanced/scroll-behavior.md)
2121
- [Lazy Loading](advanced/lazy-loading.md)
2222
- API Reference
2323
- [router-link](api/router-link.md)
2424
- [router-view](api/router-view.md)
25+
- [The Route Object](api/route-object.md)
2526
- [Router Constructor Options](api/options.md)
26-
- [Route Config Options](api/route-config.md)
27+
- [Routes Configuration](api/route-config.md)
2728
- [Router Instance](api/router-instance.md)
28-
- [The $route Object](api/route-object.md)
29-
- [Component Route Hooks](api/component-hooks.md)
29+
- [Component Injections](api/component-injections.md)

docs/en/advanced/navigation-guards.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Navigation Guards
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+
### Global Guards
6+
7+
You can register global before guards using `router.beforeEach`:
8+
9+
``` js
10+
const router = new VueRouter({ ... })
11+
12+
router.beforeEach((route, redirect, next) => {
13+
// ...
14+
})
15+
```
16+
17+
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.
18+
19+
Every guard function receives three arguments:
20+
21+
- `route: Route`: the target route object being navigated to.
22+
23+
- `redirect: Function`: calling this function will abort the current navigation and start a new navigation towards the redirect target.
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**.
26+
27+
**If neither `redirect` nor `next` is called, the navigation will be cancelled.**
28+
29+
You can also register global after hooks, however unlike guards, these hooks are much simpler and cannot affect the navigation:
30+
31+
``` js
32+
router.afterEach(route => {
33+
// ...
34+
})
35+
```
36+
37+
### Per-Route Guard
38+
39+
You can define `beforeEnter` guards directly on a route's configuration object:
40+
41+
``` js
42+
const router = new VueRouter({
43+
routes: [
44+
{
45+
path: '/foo',
46+
component: Foo,
47+
beforeEnter: (route, redirect, next) => {
48+
// ...
49+
}
50+
}
51+
]
52+
})
53+
```
54+
55+
These guards have the exact same signature as global before guards.
56+
57+
### In-Component Guards
58+
59+
Finally, you can directly define route navigation guards inside route components with `beforeRouteEnter` and `beforeRouteLeave`:
60+
61+
``` js
62+
const Foo = {
63+
template: `...`,
64+
beforeRouteEnter (route, redirect, next) => {
65+
// called before the route that renders this component is confirmed.
66+
// does NOT have access to `this` component instance,
67+
// because it has not been created yet when this guard is called!
68+
},
69+
beforeRouteLeave (route, redirect, next) => {
70+
// called when the route that renders this component is about to
71+
// be navigated away from.
72+
// has access to `this` component instance.
73+
}
74+
}
75+
```
76+
77+
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.
78+
79+
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+
81+
``` js
82+
beforeRouteEnter (route, redirect, next) => {
83+
next(vm => {
84+
// access to component instance via `vm`
85+
})
86+
}
87+
```
88+
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`.
File renamed without changes.

docs/en/essentials/matching.md renamed to docs/en/essentials/dynamic-matching.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ You can have multiple dynamic segments in the same route, and they will map to c
3636

3737
In addition to `$route.params`, the `$route` object also exposes other useful information such as `$route.query` (if there is a query in the URL), `$route.hash`, etc. You can check out the full details in the [API Reference](../api/route-object.md).
3838

39+
### Reacting to Params Changes
40+
41+
One thing to note when using routes with params is that when the user navigates from `/user/foo` to `/user/bar`, **the same component instance will be reused**. Since both routes render the same component, this is more efficient than destroying the old instance and then creating a new one. **However, this also means that the lifecycle hooks of the component will not be called**.
42+
43+
To react to params changes in the same component, you can simply watch the `$route` object:
44+
45+
``` js
46+
const User = {
47+
template: '...',
48+
watch: {
49+
'$route' (to, from) {
50+
// react to route changes...
51+
}
52+
}
53+
}
54+
```
55+
3956
### Advanced Matching Patterns
4057

4158
`vue-router` uses [path-to-regexp](https://github.com/pillarjs/path-to-regexp) as its path matching engine, so it supports many advanced matching patterns such as optional dynamic segments, zero or more / one or more requirements, and even custom regex patterns. Check out its [documentation](https://github.com/pillarjs/path-to-regexp#parameters) for these advanced patterns, and [this example](https://github.com/vuejs/vue-router/blob/next/examples/route-matching/app.js) of using them in `vue-router`.

0 commit comments

Comments
 (0)