I’m new with vue, and I don’t understand what I’m doing wrong here.
I have this simple component:
<template>
<template v-if="loading">
Loading...
</template>
<template v-else>
<div class="row">
{{ row }}
</div>
</template>
</template>
<script>
import { api } from 'src/boot/axios';
import { useUserLoginStore } from 'src/stores/UserLoginStore';
export default {
async mounted() {
this.loading = true
try {
const res = await api.get(`/v-cards/slug/${this.$route.params.slug}`, {
headers: {
Authorization: `Bearer ${useUserLoginStore().userToken}`,
}
});
this.rows = await res.data
this.loading = false
console.log('rows', this.rows)
} catch (error) {
this.error = true
console.log(error)
this.loading = false
}
},
data() {
return {
loading: false,
row: [],
}
},
}
</script>
But when I rendere the page I see only an empty array.
The api call it’s ok because I see the correct data in the console log.
>Solution :
Any particular reason why you are awaiting res.data? You’re already awaiting the api call response above. I believe removing the await in front of res.data should fix your issue.
change this line:
this.rows = await res.data
to this:
this.rows = res.data
This is assuming that res.data is exactly the array you’re expecting. and not nested in another object property.
Also in your template you should use rows not row