My Vuex store file appears very typical and contains the following code:
//store.js
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
loading: true,
companyBasicInfo: [
]
},
mutations: {
getCompanyBasicInfo: (state, res) => {
state.companyBasicInfo.push(res);
},
changeLoadingStatue: (state, loading) => {
state.loading = loading;
}
},
actions: {
getCompanyBasicInfo: context => {
// AXIOS REQUESTS GO HERE
}
}
});
The axios request in my getCompanyBasicInfo() action functions perfectly.
My Objective
I aim to separate my AXIOS requests into another file in order to streamline my store.js file.
My Attempt
To achieve this, I created a file named requests.js with the following content:
import axios from 'axios';
export default GET_COMPANY_DETAILS = () => {
setTimeout(() => {
axios.get('http://localhost:3000/companies/show/trade key egypt').then((res) => {
context.commit('getCompanyBasicInfo', res.data);
context.commit('changeLoadingStatue', false);
}).catch(e => {
console.log(e);
});
}, 3000);
};
Afterward, I attempted to import this file into my store.js file like so:
import requests from './requests';
Issue
However, I faced difficulty when trying to call requests.GET_COMPANY_DETAILS();
within the getCompanyBasicInfo()
action. It seems that the method in the requests.js
file cannot be accessed.
Error Notification
An error is displayed in the console stating
Uncaught ReferenceError: GET_COMPANY_DETAILS is not defined
.