Flora
Flora

Reputation: 563

Vuex: Can mutations contain conditional statements?

Can mutations contain conditional statements, like here?

 const mutations = {
      getResponse(state, response) {
        if (response.status === 200) {
          state.item.data = response.data;
        } else {
          state.item.errorText = response;
        }
     },

Upvotes: 0

Views: 1265

Answers (1)

Luke Ramsden
Luke Ramsden

Reputation: 734

There's nothing stopping you from doing this AFAIK, as long as it's synchronous it will run - but it's not exactly idiomatic. You'd be better-off having 2 mutations, and checking the status in your action.

const mutations = {
    responseData(state, response) {
        state.item.data = response.data;
    },
    responseError(state, response) {
        state.item.errorText = response;
    },
};

Upvotes: 2

Related Questions