Ver Fonte

also expose getters in mapState

Evan You há 8 anos atrás
pai
commit
870bdc52d5
2 ficheiros alterados com 9 adições e 4 exclusões
  1. 1 1
      src/helpers.js
  2. 8 3
      test/unit/test.js

+ 1 - 1
src/helpers.js

@@ -3,7 +3,7 @@ export function mapState (map) {
   Object.keys(map).forEach(key => {
   Object.keys(map).forEach(key => {
     const fn = map[key]
     const fn = map[key]
     res[key] = function mappedState () {
     res[key] = function mappedState () {
-      return fn.call(this, this.$store.state)
+      return fn.call(this, this.$store.state, this.$store.getters)
     }
     }
   })
   })
   return res
   return res

+ 8 - 3
test/unit/test.js

@@ -229,17 +229,22 @@ describe('Vuex', () => {
     const store = new Vuex.Store({
     const store = new Vuex.Store({
       state: {
       state: {
         a: 1
         a: 1
+      },
+      getters: {
+        b: () => 2
       }
       }
     })
     })
     const vm = new Vue({
     const vm = new Vue({
       store,
       store,
       computed: mapState({
       computed: mapState({
-        a: state => state.a + 1
+        a: (state, getters) => {
+          return state.a + getters.b
+        }
       })
       })
     })
     })
-    expect(vm.a).to.equal(2)
-    store.state.a++
     expect(vm.a).to.equal(3)
     expect(vm.a).to.equal(3)
+    store.state.a++
+    expect(vm.a).to.equal(4)
   })
   })
 
 
   it('helper: mapMutations (array)', () => {
   it('helper: mapMutations (array)', () => {