# 数据的存储方式
先看一下一个下面的例子
const ADD = 'ADD';
const storeOptions = {
modules: {
a: {
namespaced: true,
state: () => ({
nums: [],
}),
mutations: {
ADD(state, data) {
state.push(data);
}
},
actions: {
plus({ commit }, data) {
commit(ADD, data);
}
},
getters: {
maxNum(state) { return Math.max(...state.nums); }
},
modules: {
b: {
namespaced: true,
state: () => ({
nums: [],
}),
mutations: {
ADD(state, data) {
state.push(data);
}
},
actions: {
plus({ commit }, data) {
commit(ADD, data);
}
},
getters: {
maxNum(state) { return Math.max(...state.nums); }
},
modules: {
c: {
namespaced: true,
state: () => ({
nums: [],
}),
mutations: {
ADD(state, data) {
state.push(data);
}
},
actions: {
plus({ commit }, data) {
commit(ADD, data);
}
},
getters: {
maxNum(state) { return Math.max(...state.nums); }
},
}
}
}
}
}
}
}
const store = new window.Vuex.Store(window.storeOptions)
vm = new Vue({
el: '#container',
store,
data() {
console.log('store', store);
return {};
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
上面的例子的打印结果就是下面的结构
{
"_committing": false,
// 这个就是我们在嵌套的modules里定义的actions
"_actions": {
"a/plus": [null],
"a/b/plus": [null],
"a/b/c/plus": [null]
},
// 针对action的订阅者
"_actionSubscribers": [],
// 这个就是我们在嵌套modules里定义的mutations
"_mutations": {
"a/ADD": [null],
"a/b/ADD": [null],
"a/b/c/ADD": [null]
},
"_wrappedGetters": {},
// 这个会存储一个嵌套的modules
"_modules": {
"root": {
"runtime": false,
"_children": {
"a": {
"runtime": false,
"_children": {
"b": {
"runtime": false,
"_children": {
"c": {
"runtime": false,
"_children": {},
"_rawModule": {
"namespaced": true,
"mutations": {},
"actions": {},
"getters": {}
},
"state": {
"nums": []
},
"context": {}
}
},
// 这个是modules的原始数
"_rawModule": {
"namespaced": true,
"mutations": {},
"actions": {},
"getters": {},
"modules": {}
},
"state": {
"nums": []
},
"context": {}
}
},
"_rawModule": {
"namespaced": true,
"mutations": {},
"actions": {},
"getters": {},
"modules": {}
},
"state": {
"nums": []
},
"context": {}
}
},
"_rawModule": {
"modules": {}
},
"state": {},
"context": {}
}
},
"_modulesNamespaceMap": {},
"_subscribers": [],
"_makeLocalGettersCache": {
"a/": {
"maxNum": null,
"b/maxNum": null,
"b/c/maxNum": null
},
"a/b/": {
"maxNum": null,
"c/maxNum": null
},
"a/b/c/": {
"maxNum": null
}
},
"strict": false,
// 这个就是我们在嵌套modules里定义的getters
"getters": {
"a/maxNum": null,
"a/b/maxNum": null,
"a/b/c/maxNum": null
},
// 因为vuex本质也是用一个新的vue实例去管理数据的,所以这里就是对应的vue实例
"_vm": {
"_uid": 1,
"_isVue": true,
"$options": {
"components": {},
"directives": {},
"filters": {},
"beforeCreate": [null],
"computed": {}
},
"_renderProxy": {
"_uid": 1,
"_isVue": true,
"$children": [],
"$refs": {},
"_watcher": null,
"_inactive": null,
"_directInactive": false,
"_isMounted": false,
"_isDestroyed": false,
"_isBeingDestroyed": false,
"_events": {},
"_hasHookEvent": false,
"_vnode": null,
"_staticTrees": null,
"$slots": {},
"$scopedSlots": {},
"_watchers": [{
"deep": false,
"user": false,
"lazy": true,
"sync": false,
"id": 1,
"active": true,
"dirty": false,
"deps": [{
"id": 6,
"subs": [null, {
"deep": false,
"user": false,
"lazy": true,
"sync": false,
"id": 2,
"active": true,
"dirty": false,
"deps": [null, {
"id": 7,
"subs": [null, null, {
"deep": false,
"user": false,
"lazy": true,
"sync": false,
"id": 3,
"active": true,
"dirty": false,
"deps": [null, null, {
"id": 8,
"subs": [null, null, null]
}, {
"id": 9,
"subs": [null, null, null]
}, {
"id": 12,
"subs": [null, null]
}, {
"id": 13,
"subs": [null, null]
}, {
"id": 16,
"subs": [null]
}, {
"id": 17,
"subs": [null]
}, {
"id": 18,
"subs": [null]
}, {
"id": 19,
"subs": [null]
}],
"newDeps": [],
"depIds": {},
"newDepIds": {},
"expression": "function () {\n return fn(arg)\n }",
"value": null
}]
}, null, null, null, null, {
"id": 14,
"subs": [null]
}, {
"id": 15,
"subs": [null]
}],
"newDeps": [],
"depIds": {},
"newDepIds": {},
"expression": "function () {\n return fn(arg)\n }",
"value": null
}, null]
}, null, null, null, {
"id": 10,
"subs": [null]
}, {
"id": 11,
"subs": [null]
}],
"newDeps": [],
"depIds": {},
"newDepIds": {},
"expression": "function () {\n return fn(arg)\n }",
"value": null
}, null, null],
"_data": {},
"_computedWatchers": {},
"a/maxNum": null,
"a/b/maxNum": null,
"a/b/c/maxNum": null
},
"_watcher": null,
"_inactive": null,
"_directInactive": false,
"_isMounted": false,
"_isDestroyed": false,
"_isBeingDestroyed": false,
"_hasHookEvent": false,
"_vnode": null,
"_staticTrees": null,
"a/maxNum": null,
"a/b/maxNum": null,
"a/b/c/maxNum": null
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233