Skip to content

Commit 9e78ca2

Browse files
authored
docs: add example of nested named routes (#1931)
* docs: add example of nested named routes Closes #1921 * docs: add link of jsfiddle in note * review * [skip ci] review
1 parent 81e6ce0 commit 9e78ca2

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Diff for: docs/en/essentials/named-views.md

+57
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,60 @@ const router = new VueRouter({
2727
```
2828

2929
A working demo of this example can be found [here](https://jsfiddle.net/posva/6du90epg/).
30+
31+
## Nested Named Views
32+
33+
It is possible to create complex layouts using named views with nested views. When doing so, you will also need to name nested `router-view` components used. Let's take a Settings panel example:
34+
35+
```
36+
/settings/emails /settings/profile
37+
+-----------------------------------+ +------------------------------+
38+
| UserSettings | | UserSettings |
39+
| +-----+-------------------------+ | | +-----+--------------------+ |
40+
| | Nav | UserEmailsSubscriptions | | +------------> | | Nav | UserProfile | |
41+
| | +-------------------------+ | | | +--------------------+ |
42+
| | | | | | | | UserProfilePreview | |
43+
| +-----+-------------------------+ | | +-----+--------------------+ |
44+
+-----------------------------------+ +------------------------------+
45+
```
46+
47+
- `Nav` is just a regular compnonent
48+
- `UserSettings` is the view comopnent
49+
- `UserEmailsSubscriptions`, `UserProfile`, `UserProfilePreview` are nested view components
50+
51+
**Note**: _Let's forget about how the HTML/CSS should look like to represent such layout and focus on the components used_
52+
53+
The `<template>` section for `UserSettings` component in the above layout would look something like this:
54+
55+
```html
56+
<!-- UserSettings.vue -->
57+
<div>
58+
<h1>User Settings</h1>
59+
<NavBar/>
60+
<router-view/>
61+
<router-view name="helper"/>
62+
</div>
63+
```
64+
65+
_The nested view components are omitted here but you can find the complete source code for the example above [here](https://jsfiddle.net/posva/22wgksa3/)_
66+
67+
Then you can achieve the layout above with this route configuration:
68+
69+
```js
70+
{ path: '/settings',
71+
// You could also have named views at the top
72+
component: UserSettings,
73+
children: [{
74+
path: 'emails',
75+
component: UserEmailsSubscriptions
76+
}, {
77+
path: 'profile',
78+
components: {
79+
default: UserProfile,
80+
helper: UserProfilePreview
81+
}
82+
}]
83+
}
84+
```
85+
86+
A working demo of this example can be found [here](https://jsfiddle.net/posva/22wgksa3/).

0 commit comments

Comments
 (0)