问题
I tried NUXT and tried following the manual on his website. But now I'm stuck with the problem of fetching data from DJANGO.
this my code.
<template>
<div>
<ul v-for="product in products" :key="product.results.id">
<NuxtLink :to="`/${product.results.id}`">
<li>{{ product.results.id }}</li>
</NuxtLink>
</ul>
</div>
</template>
<script>
export default {
async asyncData() {
const products = await fetch(
'***/product_api/api/v1/Product/?format=json'
).then((res) => res.json())
// .then(productmap => console.log(productmap.results[0].name))
return { products }
}
}
</script>
and my JSON
{
"count": 46786,
"next": "***/product_api/api/v1/Product/?limit=100&offset=100",
"previous": null,
"results": [
{
"id": 2,
"catID": "TOO08",
"catname": "เครื่องมือช่าง",
"sku": "1690",
"name": "เส้นเอ็น SL NO.50",
"brand": "ระกา",
"price": "40",
"detail": "เส้นเอ็น SL NO.50",
"imgurl1": "****",
"imgurl2": "****",
"productclick": "*****"
},
I tried following the guide to the basics. But still don't understand Anyone have a way to solve this problem?
I just want to do Dynamic Pages.https://nuxtjs.org/examples/routing-dynamic-pages
回答1:
You should be iterating over products.results
array
<template>
<div>
<ul v-for="product in products.results" :key="product.id">
<NuxtLink :to="`/${product.id}`">
<li>{{ product.id }}</li>
</NuxtLink>
</ul>
</div>
</template>
来源:https://stackoverflow.com/questions/65536792/how-can-fetch-data-in-nuxt-js-on-drf