Vuex: Passing multiple parameters to an action

Diko_Mahendra
Mar 4, 2021

--

Vuex mutations take up to two arguments: state and payload. The current state of the store is passed in by Vuex automatically as the first argument and the second argument holds any parameters you pass in.

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);
}
}

Which allows you to commit your changes in actions in an identical manner:

store.commit('authenticate', {
token,
expiration,
});

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Diko_Mahendra
Diko_Mahendra

Written by Diko_Mahendra

Frontend Engineer at Telkom Indonesia

No responses yet

Write a response