When using Vuejs with Vuex as a centralized store, requests can be written in the actions section of the Vuex module. For guidance on organizing your Vuex store, refer to this link
Define the state and getters in the Vuex module
const state = {
...
};
const getters = {
...
};
In the actions object, include the code for making server requests
const actions = {
async setTrackData({ commit }, value) {
try {
const res = await axios.get("https://seo-gmbh.eu/invest/input.php");
commit("SET_TO_THE_FINAL_DESTINATION", value);
...
} catch (error) {
...
}
},
};
When changing the state of Vuex through actions, commit the changes using the mutation function from the mutations object.
const mutations = {
/* sets the data to the state value from the state object */
SET_TO_FINAL_DESTINATION(state, data) {
...
},
};
To access the Vuex store actions and getters, import the getters in the computed section and the actions at the beginning of the methods object. Here's an example,
export default Vue.component('ABC', {
...
computed: {
...mapGetters([
/* import the getters here */
'getSomeState',
]),
},
...
methods: {
...mapActions([
/* import the actions here */
'setTrackData',
]),
...
}
}
For additional structuring of API calls in a Vuejs project, check out this link