简体   繁体   English

如何使用 DRF 在 Nuxt.js 中显示动态网址

[英]How can display dynamic urls in Nuxt.js with DRF

I just got an answer on how to bring information from DRF to display on the last thread .我刚刚得到了关于如何将 DRF 中的信息显示在最后一个线程上的答案。 I want to know more.我想知道更多。

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我只想做 Dynamic Pages.https://nuxtjs.org/examples/routing-dynamic-pages

How do I get it to be displayed?如何让它显示? tree path is树路径是

pages页面

-- _catname -- _猫名

---_product.vue ----产品.vue

index.vue索引.vue

_id.vue _id.vue

Sorry for asking a lot I'm really a beginner抱歉问了很多我真的是初学者

My _product.vue我的 _product.vue

 <template>
  <div>
    <h1>CAT NAME: {{ catname }}</h1>
    <h2>Product ID: {{ id }}</h2>
    <p>Path: {{ $route.path }}</p>
    <NuxtLink to="/">Back to All Product</NuxtLink>
  </div>
</template>
<script>
export default {
  async asyncData({ params, redirect }) {
    const products = await fetch(
      'http://127.0.0.1:8000/product_api/api/v1/Product/'
    ).then((res) => res.json())

    const filteredproducts = products.results.find(
      (el) =>
        el.catname === params.catname &&
        el.id === params.id
    )
    if (filteredproducts) {
      return {
        catname: filteredproducts.catname,
        product: filteredproducts.name
      }
    } else {
      redirect('/')
    }
  }
}
</script>

and my _id.vue还有我的 _id.vue

<template>
  <h1>{{ this.id }}</h1>
</template>

<script>
  export default {
   async asyncData({ params }) {
      const id = await params.id // When calling /abc the slug will be "abc"
      return { id }
    }
  }


  
</script>

You should use this keyword in template.您应该在模板中使用this关键字。

Also let us know what's the error for you and what do you want to achieve?也让我们知道您的错误是什么以及您想要实现什么?

├─Pages
    ├────index.vue
    ├────_product.vue
    ├────_catname.vue
    ├────_id.vue

Assuming you have above file strcture.假设您有上述文件结构。 In that case _product.vue have only param- product .在这种情况下, _product.vue只有 param- product Hence, product can be filter by params.product only.因此,产品只能通过params.product过滤。 here is the code.这是代码。

<template>
  <div>
    <h1>CAT NAME: {{ catname }}</h1>
    <h2>Product ID: {{ id }}</h2>
    <p>Path: {{ $route.path }}</p>
    <NuxtLink to="/">Back to All Product</NuxtLink>
  </div>
</template>
<script>
export default {
  async asyncData({ params, redirect }) {
    const products = await fetch(
      'http://127.0.0.1:8000/product_api/api/v1/Product/'
    ).then((res) => res.json())

    const filteredproducts = products.results.find(
      (el) =>
        el.id === parseInt(params.product)
    )
    if (filteredproducts) {
      return {
        catname: filteredproducts.catname,
        product: filteredproducts.name
      }
    } else {
      redirect('/')
    }
  }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM