File tree Expand file tree Collapse file tree 2 files changed +65
-4
lines changed
Expand file tree Collapse file tree 2 files changed +65
-4
lines changed Original file line number Diff line number Diff line change @@ -5,10 +5,8 @@ export default class Module {
55 this . runtime = runtime
66 this . _children = Object . create ( null )
77 this . _rawModule = rawModule
8- }
9-
10- get state ( ) {
11- return this . _rawModule . state || { }
8+ const rawState = rawModule . state
9+ this . state = ( typeof rawState === 'function' ? rawState ( ) : rawState ) || { }
1210 }
1311
1412 get namespaced ( ) {
Original file line number Diff line number Diff line change @@ -105,6 +105,69 @@ describe('Modules', () => {
105105 } )
106106
107107 describe ( 'modules usage' , ( ) => {
108+ it ( 'state as function (multiple module in same store)' , ( ) => {
109+ const module = {
110+ state ( ) {
111+ return { a : 0 }
112+ } ,
113+ mutations : {
114+ [ TEST ] ( state , n ) {
115+ state . a += n
116+ }
117+ }
118+ }
119+
120+ const store = new Vuex . Store ( {
121+ modules : {
122+ one : module ,
123+ two : module
124+ }
125+ } )
126+
127+ expect ( store . state . one . a ) . toBe ( 0 )
128+ expect ( store . state . two . a ) . toBe ( 0 )
129+
130+ store . commit ( TEST , 1 )
131+ expect ( store . state . one . a ) . toBe ( 1 )
132+ expect ( store . state . two . a ) . toBe ( 1 )
133+ } )
134+
135+ it ( 'state as function (same module in multiple stores)' , ( ) => {
136+ const module = {
137+ state ( ) {
138+ return { a : 0 }
139+ } ,
140+ mutations : {
141+ [ TEST ] ( state , n ) {
142+ state . a += n
143+ }
144+ }
145+ }
146+
147+ const storeA = new Vuex . Store ( {
148+ modules : {
149+ foo : module
150+ }
151+ } )
152+
153+ const storeB = new Vuex . Store ( {
154+ modules : {
155+ bar : module
156+ }
157+ } )
158+
159+ expect ( storeA . state . foo . a ) . toBe ( 0 )
160+ expect ( storeB . state . bar . a ) . toBe ( 0 )
161+
162+ storeA . commit ( TEST , 1 )
163+ expect ( storeA . state . foo . a ) . toBe ( 1 )
164+ expect ( storeB . state . bar . a ) . toBe ( 0 )
165+
166+ storeB . commit ( TEST , 2 )
167+ expect ( storeA . state . foo . a ) . toBe ( 1 )
168+ expect ( storeB . state . bar . a ) . toBe ( 2 )
169+ } )
170+
108171 it ( 'module: mutation' , function ( ) {
109172 const mutations = {
110173 [ TEST ] ( state , n ) {
You can’t perform that action at this time.
0 commit comments