I am trying the new Nuxt.js Fetch method. Initially, I thought everything was fine. But the data is only fetched and rendered when I refresh the page. However, if the page is accesses through $fetchState.error equals true and the data is never fetched.
What am I doing wrong here?
<template>
<main>
<div>
<div>
<p v-if="$fetchState.pending">
Fetching vehicles...
</p>
<p v-else-if="$fetchState.error">
Error while fetching vehicles
</p>
<div
v-for="(vehicle, index) in usedVehicles"
v-else
:key="index"
>
<nuxt-link :to="`cars/${vehicle.Id}`">
{{ vehicle.Make }}
</nuxt-link>
</div>
</div>
<button @click="$fetch">Refresh Data</button>
</div>
</main>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
usedVehicles: []
}
},
async fetch() {
const { data } = await axios.get(
'https://random.com/api'
)
// `todos` has to be declared in data()
this.usedVehicles = data.Vehicles
},
methods: {
refresh() {
this.$fetch()
}
}
}
</script>
