I'm new to 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 render the page, I only see an empty array. The api call is fine as I see the correct data in the console log.
Is there any specific reason why you are waiting for
res.data? You are already waiting for the response from the api call above. I believe deletingres.dataand waiting in front should solve your problem.Change this line:
Regarding:
This is assuming
res.datais exactly the array you expect. and not nested within another object property.Also, in templates you should use
rowsinstead ofrow