반응형
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
반응형
'programing' 카테고리의 다른 글
vue 루프에서 요소의 색상을 동적으로 변경 (0) | 2022.08.29 |
---|---|
String.equals 대 == (0) | 2022.08.29 |
오류: 소켓을 주소로 바인딩하는 동안 주소가 이미 사용 중이지만 포트 번호가 'netstat'로 비어 있습니다. (0) | 2022.08.29 |
오류 발생 중 오류: [vuex] getters가 기능해야 하지만 저장소를 모듈로 분할할 때 VUEX 저장소에서 "getters.getters"가 {}입니다. (0) | 2022.08.29 |
Vue router-link가 URL을 변경하지만 라우터 뷰 컴포넌트는 변경하지 않음 (0) | 2022.08.29 |