[Vuejs]-Vuex state module structure

0👍

From docs:

Inside a module’s mutations and getters, the first argument received will be the module’s local state.

So your mutation should access the module object:

const mutations = {
    setUser(state, user){
        state.user = user; // Assuming you have a user module **
    }
};

** Assuming you have a user module this way:

const store = new Vuex.Store({
   modules: {
       user: moduleUser,
       // more modules ...
   }
})

Mutations should only modified the state. You should change your logic to fetch user data from an action only and not from your mutation itself, for example:

const actions = {
    currentUser: ({commit}) => {
        axios.get('/user').then(response => {
            if(response.status == 200){
                var response = response.data.data;
                commit('setUser', response);
            }
        }).catch(response =>  {

        });

    }
}

Leave a comment