Jelajahi Sumber

expose rootState in module getters and actions

Evan You 8 tahun lalu
induk
melakukan
2e46bf9891
2 mengubah file dengan 11 tambahan dan 4 penghapusan
  1. 3 2
      src/index.js
  2. 8 2
      test/unit/test.js

+ 3 - 2
src/index.js

@@ -120,7 +120,8 @@ class Store {
         dispatch,
         commit,
         getters: store.getters,
-        state: getNestedState(store.state, path)
+        state: getNestedState(store.state, path),
+        rootState: store.state
       }, payload, cb)
       if (!isPromise(res)) {
         res = Promise.resolve(res)
@@ -276,7 +277,7 @@ function extractModuleGetters (getters = {}, modules = {}, path = []) {
           return
         }
         getters[getterKey] = function wrappedGetter (state) {
-          return rawGetter(getNestedState(state, modulePath))
+          return rawGetter(getNestedState(state, modulePath), state)
         }
       })
     }

+ 8 - 2
test/unit/test.js

@@ -378,9 +378,10 @@ describe('Vuex', () => {
     let calls = 0
     const makeAction = n => {
       return {
-        [TEST] ({ state }) {
+        [TEST] ({ state, rootState }) {
           calls++
           expect(state.a).to.equal(n)
+          expect(rootState).to.equal(store.state)
         }
       }
     }
@@ -424,7 +425,12 @@ describe('Vuex', () => {
 
   it('module: getters', function () {
     const makeGetter = n => ({
-      [`getter${n}`]: state => state.a
+      [`getter${n}`]: (state, rootState) => {
+        if (rootState) {
+          expect(rootState).to.equal(store.state)
+        }
+        return state.a
+      }
     })
     const store = new Vuex.Store({
       state: {