javascript - 如何在 Nuxt 中使用一个组件创建无限路由

标签 javascript vue.js routes nuxt.js

我最近收到了一个使用 Nuxt.js 的项目,但遇到了一些问题。
我有一个从 API 获取一些产品并从 Vuex 获取它们的组件。
每个产品可能有一个子数组,每个子可能有一个子数组,依此类推。

import Images from '@/components/products/Images'
import { mapGetters } from 'vuex'

export default {
  components: {
    Images
  },
  data () {
    return {
      images: [],
      options: {
        width: 300,
        padding: {
          2: 8,
          default: 12
        }
      }
    }
  },
  computed: {
    ...mapGetters({
      products: 'getProducts'
    })
  },
  mounted () {
    this.images = this.products.filter(
      item => item.slug === this.$route.params.slug
    )
  }
}
<template>
  <div class="container_pic">
    <div class="flow-root">
      <div
        v-for="(item, index) in images"
        :key="index"
        class="w-full md:p-4 py-4"
      >
        <h6 class="text-lg mt-6 mb-8">
          {{ item.title }}
        </h6>
        <Images
          :item="item"
          :slug="true"
          :childs="item.childs"
          large
        />
      </div>
    </div>
  </div>
</template>

产品数据可以是这样的:

products: {
  { id: 1, childs: [], title: "title 1" },
  { id: 2, childs: [
    { id: 3, childs: [], title: "title 3" },
    { id: 4, childs: [
      { id: 5, childs: [
        { id: 6, childs: [], title: "title 6" },
      ], title: "title 5" },
    ], title: "title 4" },
  ], title: "title 2" }
}

我需要的是每次用户单击图像组件时,如果产品有子项并且它的长度大于 0,则将子项重定向到当前路线并获取子项数组。

最佳答案

不确定我是否理解正确,但是您可能有递归路线,对吗?
Nuxt 的未知动态嵌套路由可能对 IMO 有所帮助:https://nuxtjs.org/docs/2.x/features/file-system-routing#unknown-dynamic-nested-routes
如果你像这样组织你的页面

pages/
--| people/
-----| _id.vue
-----| index.vue
--| _.vue
--| index.vue
您将获得以下路线
/ -> index.vue
/people -> people/index.vue
/people/123 -> people/_id.vue
/about -> _.vue
/about/careers -> _.vue
/about/careers/chicago -> _.vue

关于javascript - 如何在 Nuxt 中使用一个组件创建无限路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68737439/

相关文章:

ruby-on-rails - 找不到 xyzController 的操作 x

php - 如何将变量传递给中间件函数? [SLIM框架]

javascript - 如何在angular js中从前端获取下拉列表的选定值

javascript - 如何使用 Google map 事件监听器传递变量并提交表单

javascript - 使用 jquery validate() 同时具有提交处理程序

vue.js - Vuetify-默认情况下如何使第一个扩展面板保持打开状态,如果我打开另一个面板,则其他面板应该关闭?

ruby-on-rails - "resources :post, except: :new"使 Rails 认为路线/帖子/新将指向帖子 ID "new"

javascript - 如何对使用数据列表的文本输入的 keyup 和项目选择执行操作?

javascript - 捕获单元测试所需属性的 Vue.js 警告

javascript - 从 WP API 传递一个值来做 Vue href?