From: skirtle <65301168+skirtles-code@users.noreply.github.com> Date: Tue, 6 Feb 2024 08:13:39 +0000 (+0000) Subject: docs: explain per-route guards on nested routes (#2130) X-Git-Tag: v4.3.0~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3025e826e96e439cd8e1ad5a2a4bf5b1b45b1265;p=thirdparty%2Fvuejs%2Frouter.git docs: explain per-route guards on nested routes (#2130) --- diff --git a/packages/docs/guide/advanced/navigation-guards.md b/packages/docs/guide/advanced/navigation-guards.md index 1d0067f6..57884d3c 100644 --- a/packages/docs/guide/advanced/navigation-guards.md +++ b/packages/docs/guide/advanced/navigation-guards.md @@ -197,7 +197,28 @@ const routes = [ ] ``` -Note it is possible to achieve a similar behavior by using [route meta fields](./meta.md) and global navigation guards. +When working with [nested routes](../essentials/nested-routes), both parent and child routes can use `beforeEnter`. When placed on a parent route, it won't be triggered when moving between children with that same parent. For example: + +```js +const routes = [ + { + path: '/user', + beforeEnter() { + // ... + }, + children: [ + { path: 'list', component: UserList }, + { path: 'details', component: UserDetails }, + ], + }, +] +``` + +The `beforeEnter` in the example above won't be called when moving between `/user/list` and `/user/details`, as they share the same parent. If we put the `beforeEnter` guard directly on the `details` route instead, that would be called when moving between those two routes. + +::: tip +It is possible to achieve similar behavior to per-route guards by using [route meta fields](./meta) and global navigation guards. +::: ## In-Component Guards diff --git a/packages/docs/guide/essentials/nested-routes.md b/packages/docs/guide/essentials/nested-routes.md index c4824133..fd3764b1 100644 --- a/packages/docs/guide/essentials/nested-routes.md +++ b/packages/docs/guide/essentials/nested-routes.md @@ -131,7 +131,7 @@ const routes = [ ## Omitting parent components -We can also take advantage of the parent-child relationship between routes without needing to nest route components. This can be useful for grouping together routes with a common path prefix, or when working with [route meta fields](../advanced/meta). +We can also take advantage of the parent-child relationship between routes without needing to nest route components. This can be useful for grouping together routes with a common path prefix, or when working with more advanced features, such as [per-route navigation guards](../advanced/navigation-guards#Per-Route-Guard) or [route meta fields](../advanced/meta). To achieve this, we omit the `component` and `components` options from the parent route: