gpt4 book ai didi

javascript - 获取所有嵌套内容文件并按 Nuxt 内容中的目录对它们进行分组

转载 作者:行者123 更新时间:2023-12-03 08:02:55 27 4
gpt4 key购买 nike

我有一个名为文章的博文列表我从内容文件夹中调用。

  async asyncData ({ $content }) {
const articles = await $content('', { deep: true })
// .where({ cat: 'A' })
.only(['title', 'description', 'img', 'slug', 'cat'])
.sortBy('createdAt', 'asc')
.fetch()

return { articles }
}

这会获取我的所有文章并返回它们的列表。

enter image description here

现在,我想按照以下结构在我的网站中填充不同的类别:

<template>
<div class="container">
<div v-for="article in articles" :key="article">
{{ article.title }}
</div>
<pre> {{ articles }}</pre>

<div v-for="accordion in accordions" :key="accordion.title" class="l">
<Item>
<template #title>
{{ accordion.title }}
</template>

<template #content>
<div> {{ accordion.text }}</div>
</template>
</Item>
</div>
<!-- here goes R -->
<div class="r" />
</div>
</template>

<script>
import Item from '../components/List-item.vue'
export default {
components: { Item },
async asyncData ({ $content }) {
const articles = await $content('', { deep: true })
// .where({ cat: 'A' })
.only(['title', 'description', 'img', 'slug', 'cat'])
.sortBy('createdAt', 'asc')
.fetch()

return { articles }
},
data () {
return {
accordions: [
{
title: 'A',
text: 'Projects from content/A'
},
{
title: 'B',
text: 'Projects from content/B'
},
{
title: 'C',
text: 'Projects from content/C'
}
]
}
</script>

它使用组件中的插槽:

<template>
<div class="wrapper">
<div
:class="{ active: isActive }"
@click="toggle"
>
<a class="title">
<slot name="title" />
</a>

<div v-show="show" :class="{ active: isActive }" class="content">
<slot name="content" />
</div>
</div>
</div>
</template>

我已将数组嵌套到 v-for 中,但我不知道如何按 URL 或按类别动态对该数组进行分组。

最佳答案

如果目的是查看以“cat”分隔的 Accordion 中查询的文章列表,您可以在数据中扩充 Accordion 数据结构以包含在查询中找到的文章...

async asyncData ({ $content }) {
const articles = await $content('', { deep: true })
.only(['title', 'description', 'img', 'slug', 'cat'])
.sortBy('createdAt', 'asc')
.fetch()

// set accordions as an array of grouped articles
const groups = articles.reduce((acc, article) => {
if (!acc[article.cat]) acc[article.cat] = {
title: article.cat,
text: `Articles about ${article.cat}`,
articles: [] // <-- gather the articles for this cat here
};
acc[article.cat].articles.push(article);
return acc;
}, {});
this.accordions = Object.values(groups);
}

标记应该迭代数据中的accordion(实际上是类别),然后在每个类别中嵌套迭代文章...

<div v-for="accordion in accordions" :key="accordion.title" class="l">
<Item>
<template #title>
{{ accordion.title }}
</template>

<template #content>
<div> {{ accordion.text }}</div>
<ul>
<li v-for="(article, i) in accordion.articles" :key="i">
{{article.title}}
</li>
</ul>
</template>
</Item>
</div>

关于javascript - 获取所有嵌套内容文件并按 Nuxt 内容中的目录对它们进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73436865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com