【发布时间】:2021-10-14 15:29:07
【问题描述】:
我最近收到了一个使用 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,则将子项重定向到当前路由并获取子数组。
【问题讨论】:
标签: javascript vue.js routes nuxt.js