Vuex - passing multiple parameters to action

≯℡__Kan透↙ 提交于 2019-12-02 16:27:54

Mutations expect two arguments: state and payload, where the current state of the store is passed by Vuex itself as the first argument and the second argument holds any parameters you need to pass.
The easiest way to pass a number of parameters is to destruct them:

mutations: {
    authenticate(state, { token, expiration }) {
        localStorage.setItem('token', token)
        localStorage.setItem('expiration', expiration)
    }
}

Then later on in your actions you can simply

commit('authenticate', {
    token,
    expiration
})

In simple terms you need to build your payload into a key array

payload = {'key1': 'value1', 'key2': 'value2'}

Then send the payload directly to the action

`this.$store.dispatch('yourAction', payload)`

no change in you action

    yourAction: ({commit}, payload) => {
       commit('YOUR_MUTATION',  payload )
  },

in your mutation call the values with the key

  'YOUR_MUTATION' (state,  payload ){
state.state1 = payload.key1
state.state2 =  payload.key2

},

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!