programing

이.$store.dispatch().then()은 Vuex에서 작동하지 않습니다.

minecode 2022. 8. 25. 23:06
반응형

이.$store.dispatch().then()은 Vuex에서 작동하지 않습니다.

프로젝트에서 VUEX를 사용하고 있는데 상태가 변경된 후 몇 가지 작업을 수행해야 합니다.다음은 스토어 모듈입니다.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
};

코드는 다음과 같습니다.

this.$store.dispatch("setActiveEvent", event).then(() => {
   //Something...
});

이렇게 하면 콘솔에서 다음 오류 메시지가 나타납니다.

Uncaught (in promise) TypeError: Cannot read property 'then' of undefined

내가 지금까지 시도한 것:

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

            resolve(true);
        });
    }
};

그리고:

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

그러나 둘 다 동일한 오류를 반환합니다.내가 뭘 잘못하고 있지?

편집:

도움이 될 경우 다음을 사용합니다.

  • vue ^2.5.16
  • vue-timeout ^1.0.6
  • vuex ^3.0.1
  • vuex-timeout ^1.0.0

언급URL : https://stackoverflow.com/questions/60776648/this-store-dispatch-then-is-not-working-in-vuex

반응형