简体   繁体   中英

mutation not commited vuex

I'm trying to commit a simple mutation using nuxtServerInit and I can't seem to do it.

My code is as follows:


// initial state
export const state = () => ({
  number: 0
});

// getters
export const getters = {};

// mutations
export const mutations = {
  addNumber(state) {
    console.log("addNumber mutation");
    state.number++;
  }
};

// actions
export const actions = {
  async nuxtServerInit(context) {
    console.log("nuxtServerInit");
    await context.commit("addNumber");
  },
  addNumber(context) {
    console.log("addNumber action");
    context.commit("addNumber");
  }
};

export default { actions };

The console.log("nuxtServerInit"); is called, but the mutation isn't.

Also I'm getting this error : [vuex] unknown mutation type: addNumber

You are not exporting anything other than actions . You need to export all four items. Change your last line to

export default {
    state,
    getters,
    actions,
    mutations
}

This will export mutations , getters , and state along with your actions and your problem will be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM