小编Ale*_*lho的帖子

this.$store.dispatch().then() 在 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)

vue.js vuex

7
推荐指数
0
解决办法
9284
查看次数

标签 统计

vue.js ×1

vuex ×1