How can fetch data in NUXT.JS on DRF

瘦欲@ 提交于 2021-01-24 07:11:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!