Просмотр исходного кода

also support object format dispatch

Evan You 9 лет назад
Родитель
Сommit
f07adcae6a
2 измененных файлов с 23 добавлено и 0 удалено
  1. 5 0
      src/index.js
  2. 18 0
      test/unit/test.js

+ 5 - 0
src/index.js

@@ -77,6 +77,11 @@ class Store {
    */
 
   dispatch (type, ...payload) {
+    // compatibility for object actions, e.g. FSA
+    if (typeof type === 'object' && type.type && arguments.length === 1) {
+      payload = [type]
+      type = type.type
+    }
     const mutation = this._mutations[type]
     const prevSnapshot = this._prevSnapshot
     const state = this.state

+ 18 - 0
test/unit/test.js

@@ -386,4 +386,22 @@ describe('Vuex', () => {
       done()
     })
   })
+
+  it('object-format mutations', () => {
+    const store = new Vuex.Store({
+      state: {
+        a: 1
+      },
+      mutations: {
+        [TEST] (state, action) {
+          state.a += action.by
+        }
+      }
+    })
+    store.dispatch({
+      type: TEST,
+      by: 2
+    })
+    expect(store.state.a).to.equal(3)
+  })
 })