I’m working on a Vue.js project where I need to fetch data from an external API service. When I test the API endpoint directly, it returns the expected data. However, when I try to display this data in my Vue component, nothing shows up on the page.
I can see the data in the console log, but it’s not rendering in my template. I suspect there might be an issue with how I’m handling the data structure or my loop logic.
<template>
<div v-for="item in dataList" :key="item.id">
{{ item.region }}
{{ item.cases }}
</div>
</template>
<script>
export default {
data() {
return {}
},
computed: {
dataList() {
return this.$store.state.dataList
}
},
mounted() {
this.$store.dispatch("fetchData");
}
};
</script>
import axios from 'axios'
const settings = {
method: 'GET',
url: 'https://health-api.p.rapidapi.com/statistics',
params: {country: 'france'},
headers: {
'x-rapidapi-key': 'your-api-key-here',
'x-rapidapi-host': 'health-api.p.rapidapi.com'
}
};
const healthStore = {
state: () => ({
dataList: []
}),
mutations:{
setData(state, results){
state.dataList = results
}
},
actions: {
fetchData({ commit }) {
axios.request(settings)
.then(response => {
commit('setData', response.data)
console.log(response.data)
})
.catch(function (error) {
console.error(error);
});
}
}
}
export default healthStore;
The console shows the API response is coming through correctly, but the template remains empty. What could be causing this rendering issue?