【发布时间】:2018-05-31 04:17:18
【问题描述】:
我一直在研究,到目前为止还没有找到答案。现在我有一个包含 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
}
现在我有一个动作 requestInformation,在另一个名为 app.vue 的文件上我这样称呼它。
created(){
this.$store.dispatch('requestInformation');
}
我想知道是否可以从 vuex 本身向 requestInformation 发送/发出请求,而不必在文件之外调用它,因此一旦启动全局存储,就会调用 requestInformation。
我试图避免从不同文件中分派操作。
【问题讨论】: