Skip to content

Commit 350b09e

Browse files
committed
add docs for nested vuex modules
1 parent 50cb8dc commit 350b09e

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

docs/state.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# State management
22

3-
- [Modules](#modules)
4-
- [Helpers](#helpers)
3+
- [State management](#state-management)
4+
- [Modules](#modules)
5+
- [Helpers](#helpers)
6+
- [Module Nesting](#module-nesting)
57

68
## Modules
79

@@ -26,3 +28,39 @@ export default {
2628
},
2729
}
2830
```
31+
32+
## Module Nesting
33+
34+
Vuex modules can be nested, which sometimes makes sense for organizational purposes. For example, if you created these files:
35+
36+
```js
37+
// @file src/state/modules/dashboard.js
38+
39+
export const state = {
40+
role: 'project-manager',
41+
}
42+
```
43+
44+
```js
45+
// @file src/state/modules/dashboard/videos.js
46+
47+
export const state = {
48+
all: [],
49+
}
50+
51+
export const getters = {
52+
favorited(state) {
53+
return state.all.filter((video) => video.favorited)
54+
},
55+
}
56+
```
57+
58+
Then you'd be able to access those modules with:
59+
60+
```js
61+
store.state.dashboard.role
62+
store.state.dashboard.videos.all
63+
store.getters['dashboard/videos/favorited']
64+
```
65+
66+
As you can see, placing the `videos` module in a folder called `dashboard` automatically nests it underneath the `dashboard` namespace. This works even if a `dashboard.js` file doesn't exist. You can also have as many levels of nesting as you want.

0 commit comments

Comments
 (0)