【发布时间】:2020-12-25 09:01:29
【问题描述】:
我正在使用 Nuxt Content 编写我的博客,它可以根据帖子的属性过滤帖子。我的属性之一是tags。现在我想为每个标签创建一个页面。
我当前的解决方案有效,但查询区分大小写,我真的希望它不区分大小写。
<script>
export default {
async asyncData({ $content, params }) {
const tag = params.tag
const articles = await $content('blog', params.slug)
.where({ tags: { $contains: tag } })
.only(['title', 'slug', 'description', 'createdAt', 'body'])
.sortBy('createdAt', 'asc')
.fetch()
return { articles, tag }
},
}
</script>
基于LokiJS documentation,我尝试在where 函数中使用一个函数,但这会返回所有帖子,而不仅仅是给定标签的帖子。
<script>
export default {
async asyncData({ $content, params }) {
const tag = params.tag
const articles = await $content('blog', params.slug)
.where(function (article) {
return article.tags
.map((tag) => tag.toLowerCase())
.contains(params.tag.toLowerCase())
})
.only(['title', 'slug', 'description', 'createdAt', 'body'])
.sortBy('createdAt', 'asc')
.fetch()
return { articles, tag }
},
}
</script>
那么我应该如何编写查询以获得包含标签的文章而不必担心区分大小写。
【问题讨论】: