【发布时间】:2021-12-02 15:46:34
【问题描述】:
我无法检索包含 nuxt 内容的上一篇和下一篇文章。
在我的 slug 页面中,我有以下代码:
async asyncData({ $content, params }) {
const article = await $content('onstep', params.slug).fetch();
const [prev, next] = await $content('onstep')
.only(['title', 'slug', 'order'])
.sortBy('order', 'desc')
.surround(params.slug)
.fetch();
return {
article,
prev,
next
};
},
但是 prev 和 next 返回 null,我不明白的另一件事是
.surround(params.slug)
为什么要用params.slug???
如果我使用邮递员http://localhost:3000/_content/onstep?only=title&only=slug&only=order 我会得到我的文章列表,在我的例子中,我使用一个名为 order 的标签来排序应该如何看到内容。
在我的项目中,我需要使用trailingSlash 到true,但如果我的console.log params.slug 没有trailingSlash
有什么建议吗?
【问题讨论】:
-
到目前为止,参数不应包含任何尾部斜杠。您可能正在寻找
this.$route.path?另外,你有这个minimal reproducible example 吗? -
如果我使用 `this.$route.path` 我收到错误 asyncData 中的意外情况,我不知道如何以及在哪里可以向您发送一个最小的示例项目。我看到的奇怪的事情,如果我删除环绕方法,我会按预期得到所有文章的列表......
-
如果你使用
asyncData,你需要使用上下文,因此这样写:` asyncData({ route }) {`。对于最小的项目,请分享您的 github 存储库或创建一个新项目来重现该问题。
标签: nuxt.js nuxt-content