I have a Mixins that retrieves data from the sessionStorage, which is then utilized in all components. The API call to fetch the data is located in the Main component (App.vue), where it fetches and stores the data into sessionStorage.
beforeCreate() {
if (!sessionStorage.getItem('constants')) {
axios.get('/data').then(function(response) {
sessionStorage.setItem('constants',JSON.stringify(response.data.result));
});
},
In the Mixins, I am unable to retrieve data directly from the sessionStorage due to the timing of when Mixins are executed compared to the App.vue Component.
I attempted to include the fetch call within the Mixins, but encountered issues with multiple calls being made even though I had implemented conditionals while fetching data from sessionStorage.
import Vue from 'vue';
const Constants = Vue.mixin({
data() {
const constant = JSON.parse(sessionStorage.getItem('constants'));
return {
get constantsData() {
return {
COUNTRY: constant.COUNTRY,
STATE: constant.STATE,
};
},
};
},
});
export default Constants;
What is the most effective approach for utilizing Mixins with API data?