programing

Vue 인스턴스 시작 시 Vuex 자체 내에서 액션을 디스패치할 수 있습니까?

prostudy 2022. 8. 29. 21:23
반응형

Vue 인스턴스 시작 시 Vuex 자체 내에서 액션을 디스패치할 수 있습니까?

제가 조사를 해봤는데 아직 답을 못 찾았어요.현재 2개의 모듈을 가진 스토어 인스턴스가 있지만 첫 번째 모듈을 예로 사용합니다.

/Store/index.js

import Vuex from 'vuex';
Vue.use(Vuex);

const store = new Vuex.Store({
    modules: {
        header: require('./modules/header/index').default,
        current_user: require('./modules/user/index').default,
    }
});

export default store

/Store/modules/user/index.js

const state = {
    information: [],
}

const mutations = {
    setInformation(state, data){
        state.information = data;
    }
}

const getters = {
    getFirstName(state){
        return state.information.first_name;
    },
    getProfileImage(state){
        return state.information.image;
    }
}

const actions = {
    requestInformation({commit}){
        axios.get('/app/account/requestUserInformation').then(response => commit('setInformation', response.data) );
    }
}

export default {
    state,
    mutations,
    getters,
    actions
}

현재 action requestInformation과 app이라는 이름의 다른 파일이 있습니다.vue 난 그렇게 부르고 있어.

created(){
    this.$store.dispatch('requestInformation');
}

vuex 자체에서 request Information을 dispatch/발신할 수 있는지 궁금해서 글로벌 스토어가 시작되자마자 request Information이 호출됩니다.

다른 파일에서 액션을 디스패치할 필요가 없도록 하고 있습니다.

스토어를 작성하는 즉시 액션을 디스패치할 수 있습니다./Store/index.js다음과 같습니다.

import Vuex from 'vuex';
Vue.use(Vuex);

const store = new Vuex.Store({
    modules: {
        header: require('./modules/header/index').default,
        current_user: require('./modules/user/index').default,
    }
});

store.dispatch('requestInformation');

export default store

언급URL : https://stackoverflow.com/questions/47858006/is-it-possible-to-dispatch-an-action-inside-vuex-itself-when-the-vue-instance-is

반응형