@@ -236,21 +236,21 @@ function installModule (store, rootState, path, module, hot) {
236
236
} )
237
237
}
238
238
239
- const local = module . context = makeLocalContext ( store , namespace )
239
+ const local = module . context = makeLocalContext ( store , namespace , path )
240
240
241
241
module . forEachMutation ( ( mutation , key ) => {
242
242
const namespacedType = namespace + key
243
- registerMutation ( store , namespacedType , mutation , path )
243
+ registerMutation ( store , namespacedType , mutation , local )
244
244
} )
245
245
246
246
module . forEachAction ( ( action , key ) => {
247
247
const namespacedType = namespace + key
248
- registerAction ( store , namespacedType , action , local , path )
248
+ registerAction ( store , namespacedType , action , local )
249
249
} )
250
250
251
251
module . forEachGetter ( ( getter , key ) => {
252
252
const namespacedType = namespace + key
253
- registerGetter ( store , namespacedType , getter , local , path )
253
+ registerGetter ( store , namespacedType , getter , local )
254
254
} )
255
255
256
256
module . forEachChild ( ( child , key ) => {
@@ -259,10 +259,10 @@ function installModule (store, rootState, path, module, hot) {
259
259
}
260
260
261
261
/**
262
- * make localized dispatch, commit and getters
262
+ * make localized dispatch, commit, getters and state
263
263
* if there is no namespace, just use root ones
264
264
*/
265
- function makeLocalContext ( store , namespace ) {
265
+ function makeLocalContext ( store , namespace , path ) {
266
266
const noNamespace = namespace === ''
267
267
268
268
const local = {
@@ -299,10 +299,17 @@ function makeLocalContext (store, namespace) {
299
299
}
300
300
}
301
301
302
- // getters object must be gotten lazily
303
- // because store.getters will be changed by vm update
304
- Object . defineProperty ( local , 'getters' , {
305
- get : noNamespace ? ( ) => store . getters : ( ) => makeLocalGetters ( store , namespace )
302
+ // getters and state object must be gotten lazily
303
+ // because they will be changed by vm update
304
+ Object . defineProperties ( local , {
305
+ getters : {
306
+ get : noNamespace
307
+ ? ( ) => store . getters
308
+ : ( ) => makeLocalGetters ( store , namespace )
309
+ } ,
310
+ state : {
311
+ get : ( ) => getNestedState ( store . state , path )
312
+ }
306
313
} )
307
314
308
315
return local
@@ -331,21 +338,21 @@ function makeLocalGetters (store, namespace) {
331
338
return gettersProxy
332
339
}
333
340
334
- function registerMutation ( store , type , handler , path ) {
341
+ function registerMutation ( store , type , handler , local ) {
335
342
const entry = store . _mutations [ type ] || ( store . _mutations [ type ] = [ ] )
336
343
entry . push ( function wrappedMutationHandler ( payload ) {
337
- handler ( getNestedState ( store . state , path ) , payload )
344
+ handler ( local . state , payload )
338
345
} )
339
346
}
340
347
341
- function registerAction ( store , type , handler , local , path ) {
348
+ function registerAction ( store , type , handler , local ) {
342
349
const entry = store . _actions [ type ] || ( store . _actions [ type ] = [ ] )
343
350
entry . push ( function wrappedActionHandler ( payload , cb ) {
344
351
let res = handler ( {
345
352
dispatch : local . dispatch ,
346
353
commit : local . commit ,
347
354
getters : local . getters ,
348
- state : getNestedState ( store . state , path ) ,
355
+ state : local . state ,
349
356
rootGetters : store . getters ,
350
357
rootState : store . state
351
358
} , payload , cb )
@@ -363,14 +370,14 @@ function registerAction (store, type, handler, local, path) {
363
370
} )
364
371
}
365
372
366
- function registerGetter ( store , type , rawGetter , local , path ) {
373
+ function registerGetter ( store , type , rawGetter , local ) {
367
374
if ( store . _wrappedGetters [ type ] ) {
368
375
console . error ( `[vuex] duplicate getter key: ${ type } ` )
369
376
return
370
377
}
371
378
store . _wrappedGetters [ type ] = function wrappedGetter ( store ) {
372
379
return rawGetter (
373
- getNestedState ( store . state , path ) , // local state
380
+ local . state , // local state
374
381
local . getters , // local getters
375
382
store . state , // root state
376
383
store . getters // root getters
0 commit comments