this.$store.dispatch().then() 在 Vuex 中不起作用

Ale*_*lho 7 vue.js vuex

我在我的项目中使用 VUEX,我需要在状态更改后执行一些操作。这是我在store.js上的模块:

const state = {
    activeEvent: null
};

const mutations = {
    SET_ACTIVE_EVENT(state, activeEvent) {
        state.activeEvent = activeEvent;
    }
};

const actions = {
    setActiveEvent({ commit }, activeEvent) {
        commit("SET_ACTIVE_EVENT", activeEvent);
    }
};

export default {
    state,
    mutations,
    actions
};
Run Code Online (Sandbox Code Playgroud)

这是代码:

this.$store.dispatch("setActiveEvent", event).then(() => {
   //Something...
});
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我从控制台收到以下错误消息:

Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的:

const actions = {
    setActiveEvent({ commit }, activeEvent) {
        return new Promise(resolve => {
            commit("SET_ACTIVE_EVENT", activeEvent);

            resolve(true);
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

和:

const actions = {
    setActiveEvent({ commit }, activeEvent) {
        return new Promise(resolve => {
            resolve(commit("SET_ACTIVE_EVENT", activeEvent));
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

但两者都返回相同的错误。我究竟做错了什么?

编辑:

如果有帮助,我正在使用:

  • vue^2.5.16
  • vue-电子^1.0.6
  • vuex^3.0.1
  • vuex-电子^1.0.0