forked from vuejs/vuex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.html
669 lines (374 loc) · 39.3 KB
/
modules.html
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
<!DOCTYPE HTML>
<html lang="en" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Modules · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.2">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-vuejs/vue.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="structure.html" />
<link rel="prev" href="actions.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" >
<a target="_blank" href="https://github.com/vuejs/vuex/tree/1.0/docs">
Looking for 1.0 Docs?
</a>
</li>
<li class="chapter " data-level="1.3" >
<a target="_blank" href="https://github.com/vuejs/vuex/releases">
Release Notes
</a>
</li>
<li class="chapter " data-level="1.4" data-path="installation.html">
<a href="installation.html">
Installation
</a>
</li>
<li class="chapter " data-level="1.5" data-path="intro.html">
<a href="intro.html">
What is Vuex?
</a>
</li>
<li class="chapter " data-level="1.6" data-path="getting-started.html">
<a href="getting-started.html">
Getting Started
</a>
</li>
<li class="chapter " data-level="1.7" >
<span>
Core Concepts
</span>
<ul class="articles">
<li class="chapter " data-level="1.7.1" data-path="state.html">
<a href="state.html">
State
</a>
</li>
<li class="chapter " data-level="1.7.2" data-path="getters.html">
<a href="getters.html">
Getters
</a>
</li>
<li class="chapter " data-level="1.7.3" data-path="mutations.html">
<a href="mutations.html">
Mutations
</a>
</li>
<li class="chapter " data-level="1.7.4" data-path="actions.html">
<a href="actions.html">
Actions
</a>
</li>
<li class="chapter active" data-level="1.7.5" data-path="modules.html">
<a href="modules.html">
Modules
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.8" data-path="structure.html">
<a href="structure.html">
Application Structure
</a>
</li>
<li class="chapter " data-level="1.9" data-path="plugins.html">
<a href="plugins.html">
Plugins
</a>
</li>
<li class="chapter " data-level="1.10" data-path="strict.html">
<a href="strict.html">
Strict Mode
</a>
</li>
<li class="chapter " data-level="1.11" data-path="forms.html">
<a href="forms.html">
Form Handling
</a>
</li>
<li class="chapter " data-level="1.12" data-path="testing.html">
<a href="testing.html">
Testing
</a>
</li>
<li class="chapter " data-level="1.13" data-path="hot-reload.html">
<a href="hot-reload.html">
Hot Reloading
</a>
</li>
<li class="chapter " data-level="1.14" data-path="api.html">
<a href="api.html">
API Reference
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >Modules</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="modules">Modules</h1>
<p>Due to using a single state tree, all state of our application is contained inside one big object. However, as our application grows in scale, the store can get really bloated.</p>
<p>To help with that, Vuex allows us to divide our store into <strong>modules</strong>. Each module can contain its own state, mutations, actions, getters, and even nested modules - it's fractal all the way down:</p>
<pre class="language-"><code class="lang-js language-"><span class="token keyword">const</span> moduleA <span class="token operator">=</span> <span class="token punctuation">{</span>
state<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
mutations<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
actions<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
getters<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">const</span> moduleB <span class="token operator">=</span> <span class="token punctuation">{</span>
state<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
mutations<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
actions<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
modules<span class="token punctuation">:</span> <span class="token punctuation">{</span>
a<span class="token punctuation">:</span> moduleA<span class="token punctuation">,</span>
b<span class="token punctuation">:</span> moduleB
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
store<span class="token punctuation">.</span>state<span class="token punctuation">.</span>a <span class="token comment" spellcheck="true">// -> moduleA's state</span>
store<span class="token punctuation">.</span>state<span class="token punctuation">.</span>b <span class="token comment" spellcheck="true">// -> moduleB's state</span>
</code></pre>
<h3 id="module-local-state">Module Local State</h3>
<p>Inside a module's mutations and getters, The first argument received will be <strong>the module's local state</strong>.</p>
<pre class="language-"><code class="lang-js language-"><span class="token keyword">const</span> moduleA <span class="token operator">=</span> <span class="token punctuation">{</span>
state<span class="token punctuation">:</span> <span class="token punctuation">{</span> count<span class="token punctuation">:</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
mutations<span class="token punctuation">:</span> <span class="token punctuation">{</span>
increment <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// state is the local module state</span>
state<span class="token punctuation">.</span>count<span class="token operator">++</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
doubleCount <span class="token punctuation">(</span>state<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> state<span class="token punctuation">.</span>count <span class="token operator">*</span> <span class="token number">2</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Similarly, inside module actions, <code class="language-">context.state</code> will expose the local state, and root state will be exposed as <code class="language-">context.rootState</code>:</p>
<pre class="language-"><code class="lang-js language-"><span class="token keyword">const</span> moduleA <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// ...</span>
actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
incrementIfOdd <span class="token punctuation">(</span><span class="token punctuation">{</span> state<span class="token punctuation">,</span> commit <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>state<span class="token punctuation">.</span>count <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'increment'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Also, inside module getters, the root state will be exposed as their 3rd argument:</p>
<pre class="language-"><code class="lang-js language-"><span class="token keyword">const</span> moduleA <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// ...</span>
getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
sumWithRootCount <span class="token punctuation">(</span>state<span class="token punctuation">,</span> getters<span class="token punctuation">,</span> rootState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> state<span class="token punctuation">.</span>count <span class="token operator">+</span> rootState<span class="token punctuation">.</span>count
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="namespacing">Namespacing</h3>
<p>By default, actions, mutations and getters inside modules are still registered under the <strong>global namespace</strong> - this allows multiple modules to react to the same mutation/action type.</p>
<p>If you want your modules to be more self-contained or reusable, you can mark it as namespaced with <code class="language-">namespaced: true</code>. When the module is registered, all of its getters, actions and mutations will be automatically namespaced based on the path the module is registered at. For example:</p>
<pre class="language-"><code class="lang-js language-"><span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
modules<span class="token punctuation">:</span> <span class="token punctuation">{</span>
account<span class="token punctuation">:</span> <span class="token punctuation">{</span>
namespaced<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
<span class="token comment" spellcheck="true">// module assets</span>
state<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">// module state is already nested and not affected by namespace option</span>
getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
isAdmin <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// -> getters['account/isAdmin']</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
login <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// -> dispatch('account/login')</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
mutations<span class="token punctuation">:</span> <span class="token punctuation">{</span>
login <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// -> commit('account/login')</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token comment" spellcheck="true">// nested modules</span>
modules<span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// inherits the namespace from parent module</span>
myPage<span class="token punctuation">:</span> <span class="token punctuation">{</span>
state<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
profile <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// -> getters['account/profile']</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token comment" spellcheck="true">// further nest the namespace</span>
posts<span class="token punctuation">:</span> <span class="token punctuation">{</span>
namespaced<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
state<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
popular <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// -> getters['account/posts/popular']</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p>Namespaced getters and actions will receive localized <code class="language-">getters</code>, <code class="language-">dispatch</code> and <code class="language-">commit</code>. In other words, you can use the module assets without writing prefix in the same module. Toggling between namespaced or not does not affect the code inside the module.</p>
<h4 id="accessing-global-assets-in-namespaced-modules">Accessing Global Assets in Namespaced Modules</h4>
<p>If you want to use global state and getters, <code class="language-">rootState</code> and <code class="language-">rootGetters</code> are passed as the 3rd and 4th arguments to getter functions, and also exposed as properties on the <code class="language-">context</code> object passed to action functions.</p>
<p>To dispatch actions or commit mutations in the global namespace, pass <code class="language-">{ root: true }</code> as the 3rd argument to <code class="language-">dispatch</code> and <code class="language-">commit</code>.</p>
<pre class="language-"><code class="lang-js language-">modules<span class="token punctuation">:</span> <span class="token punctuation">{</span>
foo<span class="token punctuation">:</span> <span class="token punctuation">{</span>
namespaced<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
getters<span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// `getters` is localized to this module's getters</span>
<span class="token comment" spellcheck="true">// you can use rootGetters via 4th argument of getters</span>
someGetter <span class="token punctuation">(</span>state<span class="token punctuation">,</span> getters<span class="token punctuation">,</span> rootState<span class="token punctuation">,</span> rootGetters<span class="token punctuation">)</span> <span class="token punctuation">{</span>
getters<span class="token punctuation">.</span>someOtherGetter <span class="token comment" spellcheck="true">// -> 'foo/someOtherGetter'</span>
rootGetters<span class="token punctuation">.</span>someOtherGetter <span class="token comment" spellcheck="true">// -> 'someOtherGetter'</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
someOtherGetter<span class="token punctuation">:</span> state <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
actions<span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// dispatch and commit are also localized for this module</span>
<span class="token comment" spellcheck="true">// they will accept `root` option for the root dispatch/commit</span>
someAction <span class="token punctuation">(</span><span class="token punctuation">{</span> dispatch<span class="token punctuation">,</span> commit<span class="token punctuation">,</span> getters<span class="token punctuation">,</span> rootGetters <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
getters<span class="token punctuation">.</span>someGetter <span class="token comment" spellcheck="true">// -> 'foo/someGetter'</span>
rootGetters<span class="token punctuation">.</span>someGetter <span class="token comment" spellcheck="true">// -> 'someGetter'</span>
<span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'someOtherAction'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// -> 'foo/someOtherAction'</span>
<span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'someOtherAction'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> root<span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// -> 'someOtherAction'</span>
<span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'someMutation'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// -> 'foo/someMutation'</span>
<span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'someMutation'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> root<span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// -> 'someMutation'</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
someOtherAction <span class="token punctuation">(</span>ctx<span class="token punctuation">,</span> payload<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h4 id="binding-helpers-with-namespace">Binding Helpers with Namespace</h4>
<p>When binding a namespaced module to components with the <code class="language-">mapState</code>, <code class="language-">mapGetters</code>, <code class="language-">mapActions</code> and <code class="language-">mapMutations</code> helpers, it can get a bit verbose:</p>
<pre class="language-"><code class="lang-js language-">computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
a<span class="token punctuation">:</span> state <span class="token operator">=</span><span class="token operator">></span> state<span class="token punctuation">.</span>some<span class="token punctuation">.</span>nested<span class="token punctuation">.</span>module<span class="token punctuation">.</span>a<span class="token punctuation">,</span>
b<span class="token punctuation">:</span> state <span class="token operator">=</span><span class="token operator">></span> state<span class="token punctuation">.</span>some<span class="token punctuation">.</span>nested<span class="token punctuation">.</span>module<span class="token punctuation">.</span>b
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
<span class="token string">'some/nested/module/foo'</span><span class="token punctuation">,</span>
<span class="token string">'some/nested/module/bar'</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>In such cases, you can pass the module namespace string as the first argument to the helpers so that all bindings are done using that module as the context. The above can be simplified to:</p>
<pre class="language-"><code class="lang-js language-">computed<span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token string">'some/nested/module'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
a<span class="token punctuation">:</span> state <span class="token operator">=</span><span class="token operator">></span> state<span class="token punctuation">.</span>a<span class="token punctuation">,</span>
b<span class="token punctuation">:</span> state <span class="token operator">=</span><span class="token operator">></span> state<span class="token punctuation">.</span>b
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token string">'some/nested/module'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>
<span class="token string">'foo'</span><span class="token punctuation">,</span>
<span class="token string">'bar'</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<h4 id="caveat-for-plugin-developers">Caveat for Plugin Developers</h4>
<p>You may care about unpredictable namespacing for your modules when you create a <a href="plugins.html">plugin</a> that provides the modules and let users add them to a Vuex store. Your modules will be also namespaced if the plugin users add your modules under a namespaced module. To adapt this situation, you may need to receive a namespace value via your plugin option:</p>
<pre class="language-"><code class="lang-js language-"><span class="token comment" spellcheck="true">// get namespace value via plugin option</span>
<span class="token comment" spellcheck="true">// and returns Vuex plugin function</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> createPlugin <span class="token punctuation">(</span>options <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>store<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// add namespace to plugin module's types</span>
<span class="token keyword">const</span> namespace <span class="token operator">=</span> options<span class="token punctuation">.</span>namespace <span class="token operator">||</span> <span class="token string">''</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span>namespace <span class="token operator">+</span> <span class="token string">'pluginAction'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="dynamic-module-registration">Dynamic Module Registration</h3>
<p>You can register a module <strong>after</strong> the store has been created with the <code class="language-">store.registerModule</code> method:</p>
<pre class="language-"><code class="lang-js language-">store<span class="token punctuation">.</span><span class="token function">registerModule</span><span class="token punctuation">(</span><span class="token string">'myModule'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">// ...</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p>The module's state will be exposed as <code class="language-">store.state.myModule</code>.</p>
<p>Dynamic module registration makes it possible for other Vue plugins to also leverage Vuex for state management by attaching a module to the application's store. For example, the <a href="https://github.com/vuejs/vuex-router-sync" target="_blank"><code class="language-">vuex-router-sync</code></a> library integrates vue-router with vuex by managing the application's route state in a dynamically attached module.</p>
<p>You can also remove a dynamically registered module with <code class="language-">store.unregisterModule(moduleName)</code>. Note you cannot remove static modules (declared at store creation) with this method.</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="actions.html" class="navigation navigation-prev " aria-label="Previous page: Actions">
<i class="fa fa-angle-left"></i>
</a>
<a href="structure.html" class="navigation navigation-next " aria-label="Next page: Application Structure">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Modules","level":"1.7.5","depth":2,"next":{"title":"Application Structure","level":"1.8","depth":1,"path":"structure.md","ref":"structure.md","articles":[]},"previous":{"title":"Actions","level":"1.7.4","depth":2,"path":"actions.md","ref":"actions.md","articles":[]},"dir":"ltr"},"config":{"plugins":["edit-link","prism","-highlight","github","-highlight","github"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"edit-link":{"label":"Edit This Page","base":"https://github.com/vuejs/vuex/tree/dev/docs"},"github":{"url":"https://github.com/vuejs/vuex/"},"prism":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-vuejs":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"en","links":{"sharing":{"facebook":false,"twitter":false}},"gitbook":"2.x.x"},"file":{"path":"modules.md","mtime":"2016-12-17T11:39:29.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2016-12-17T11:39:48.152Z"},"basePath":".","book":{"language":"en"}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
</body>
</html>