Browse Source

also expose getters in mapState

Evan You 8 năm trước cách đây
mục cha
commit
870bdc52d5
2 tập tin đã thay đổi với 9 bổ sung4 xóa
  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 => {
     const fn = map[key]
     res[key] = function mappedState () {
-      return fn.call(this, this.$store.state)
+      return fn.call(this, this.$store.state, this.$store.getters)
     }
   })
   return res

+ 8 - 3
test/unit/test.js

@@ -229,17 +229,22 @@ describe('Vuex', () => {
     const store = new Vuex.Store({
       state: {
         a: 1
+      },
+      getters: {
+        b: () => 2
       }
     })
     const vm = new Vue({
       store,
       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)
+    store.state.a++
+    expect(vm.a).to.equal(4)
   })
 
   it('helper: mapMutations (array)', () => {