programing

동적으로 등록된 모듈에서 vuex 변환에 액세스할 수 없음

prostudy 2022. 6. 2. 21:28
반응형

동적으로 등록된 모듈에서 vuex 변환에 액세스할 수 없음

vue 컴포넌트에 대해 API 데이터를 가져와 유지하는 모듈을 만들었습니다.

import Vue from 'vue';

export default {
    namespaced: true,

    state() {
        return {
            all: []
        }
    },

    getters: {
        all(state) {
            return state.all;
        }
    },

    mutations: {
        all(state, data) {
            Vue.set(state, 'all', data);
        },

        clear(state) {
            Vue.set(state, 'all', []);
        }
    },

    actions: {
        async init({ commit, state }) {
            let response = await this.axios.get('/api/v1/teams/', {
                params: {
                    includes: 'users'
                }
            });

            if (response.status === 200) {
                let data = response.data.data.filter(function (element) {
                    return element.users.length > 0;
                });

                commit('userTeamSelect/all', data, {root: true});
            } else {
                Vue.toasted.error('Could not fetch team data', {
                    duration: 10000
                });
            }
        }
    }
}

생성 시 모듈을 동적으로 등록하고 삭제하기 전에 저장소에서 모듈을 삭제하는 컴포넌트가 있습니다.

created() {
    if (this.$store.getters['userTeamSelect/all'] === undefined) {
        this.$store.registerModule('userTeamSelect', UserTeamSelectModule);
        this.$store.dispatch('userTeamSelect/init');
    }
},

beforeDestroy() {
    if (this.$store.getters['userTeamSelect/all'] !== undefined) {
         this.$store.commit('userTeamSelect/clear');
         this.$store.unregisterModule('userTeamSelect');
    }
},

이 컴포넌트가 사용되고 있는 페이지로 이동하면 콘솔에 다음 오류 메시지가 나타납니다.[vuex] unknown mutation type: userTeamSelect/all

이 변환은 다이내믹모듈의 init 액션 내에서만 호출되므로 어딘가에 문제가 있을 수 있습니다.둘 다 로컬로 전화하려고 했어요.commit('all', data)글로벌하게commit('userTeamSelect/all', data, {root:true})운도 없이

문제없이 액션을 호출할 수 있기 때문에 모듈이 vuex 스토어에 추가되었을 것입니다.돌연변이가 등록되지 않은 것 같아요?정상적으로 동작하거나 알 수 없는 액션에 대한 오류를 수신할 수 있습니다.

누가 저를 올바른 방향으로 안내해주실 수 있나요?

언급URL : https://stackoverflow.com/questions/60563917/cannot-access-vuex-mutation-in-dynamically-registered-module

반응형