Vue.js API Data Not Displaying Issue
I’m working on fetching external API data in my Vue application using an API from RapidAPI. The API call works fine when I test it directly with Postman, but when I implement it in my Vue app, nothing shows up on the screen.
I think there might be an issue with how I’m handling the data loop or the Vuex store setup. The console shows the API response correctly, but the template doesn’t render anything.
Here’s my Vue component:
<template>
<div v-for="item in dataItems" :key="item.id">
{{ item.region }}
{{ item.cases }}
</div>
</template>
<script>
export default {
data() {
return {}
},
computed: {
dataItems() {
return this.$store.state.dataItems
}
},
mounted() {
this.$store.dispatch("fetchData");
}
};
</script>
And here’s my Vuex store:
import axios from 'axios'
const requestConfig = {
method: 'GET',
url: 'https://disease-tracker-api.p.rapidapi.com/stats',
params: {country: 'spain'},
headers: {
'x-rapidapi-key': 'your-api-key-here',
'x-rapidapi-host': 'disease-tracker-api.p.rapidapi.com'
}
};
const healthData = {
state: () => ({
dataItems: []
}),
mutations:{
updateData(state, payload){
state.dataItems = payload
}
},
actions: {
fetchData({ commit }) {
axios.request(requestConfig)
.then(result => {
commit('updateData', result.data)
console.log(result.data)
})
.catch(function (err) {
console.error(err);
});
}
}
}
export default healthData;
The console.log shows the API response is coming through properly, but my template stays empty. What am I missing in my implementation?