@@ -52,11 +52,23 @@ export function initVuexBackend (hook, bridge, isLegacy) {
52
52
function addModule ( path , module , options ) {
53
53
if ( typeof path === 'string' ) path = [ path ]
54
54
55
+ let state
56
+ if ( options && options . preserveState ) {
57
+ state = get ( store . state , path )
58
+ }
59
+ if ( ! state ) {
60
+ state = typeof module . state === 'function' ? module . state ( ) : module . state
61
+ }
62
+
55
63
const key = path . join ( '/' )
56
64
registeredModules [ key ] = allTimeModules [ key ] = {
57
65
path,
58
66
module,
59
- options
67
+ options : {
68
+ ...options ,
69
+ preserveState : false
70
+ } ,
71
+ state : stringify ( state )
60
72
}
61
73
62
74
if ( SharedData . recordVuex ) {
@@ -250,9 +262,13 @@ export function initVuexBackend (hook, bridge, isLegacy) {
250
262
for ( let i = stateSnapshot . index + 1 ; i <= index ; i ++ ) {
251
263
const mutation = mutations [ i ]
252
264
if ( mutation . registerModule ) {
253
- const { path, module, options } = mutation . payload
254
- tempAddedModules . push ( path . join ( '/' ) )
255
- origRegisterModule ( path , module , options )
265
+ const key = mutation . payload . path . join ( '/' )
266
+ const registeredModule = registeredModules [ key ]
267
+ tempAddedModules . push ( key )
268
+ origRegisterModule ( registeredModule . path , {
269
+ ...registeredModule . module ,
270
+ state : parse ( registeredModule . state , true )
271
+ } , registeredModule . options )
256
272
updateSnapshotsVm ( store . state )
257
273
} else if ( mutation . unregisterModule && get ( store . state , mutation . payload . path ) != null ) {
258
274
const path = mutation . payload . path
@@ -296,8 +312,11 @@ export function initVuexBackend (hook, bridge, isLegacy) {
296
312
origUnregisterModule ( m . split ( '/' ) )
297
313
} )
298
314
tempRemovedModules . sort ( ( a , b ) => a . length - b . length ) . forEach ( m => {
299
- const { path, module, options } = registeredModules [ m ]
300
- origRegisterModule ( path , module , options )
315
+ const { path, module, options, state } = registeredModules [ m ]
316
+ origRegisterModule ( path , {
317
+ ...module ,
318
+ state : parse ( state , true )
319
+ } , options )
301
320
} )
302
321
store . _vm = originalVm
303
322
@@ -356,8 +375,11 @@ export function initVuexBackend (hook, bridge, isLegacy) {
356
375
if ( ! Object . keys ( registeredModules ) . sort ( ( a , b ) => a . length - b . length ) . includes ( m ) ) {
357
376
const data = allTimeModules [ m ]
358
377
if ( data ) {
359
- const { path, module, options } = data
360
- origRegisterModule ( path , module , options )
378
+ const { path, module, options, state } = data
379
+ origRegisterModule ( path , {
380
+ ...module ,
381
+ state : parse ( state , true )
382
+ } , options )
361
383
registeredModules [ path . join ( '/' ) ] = data
362
384
}
363
385
}
0 commit comments