nuxt asyncdata返回带有NUXT内容的JSON5文件的空数组
我正在使用Asyncdata方法检索JSON5文件的内容,并且我会得到一个空数组。
我的文件位于content/myfile.json5上的
内容是:
[
{
id: 1
},
{
id: 21
}
]
我的方法是:
async asyncData({ $content}) {
const test = await $content('myfile').fetch()
return {
test
}
}
如果我使用嵌套在目录中的常规.json文件,例如/content/places/places.json,我也无法获取数组。 。 结果是一个装有我数组的第一项的对象。看起来只有在其位于根内容文件夹中的情况下才起作用。 我找不到文档中的任何解释。
I am using the asyncData method to retrieve the content of a json5 file and I get an empty array.
My file is located at content/myfile.json5
The content is :
[
{
id: 1
},
{
id: 21
}
]
And my method is :
async asyncData({ $content}) {
const test = await $content('myfile').fetch()
return {
test
}
}
I am also not able to fetch an array if I use a regular .json file nested in a directory, like /content/places/places.json.
The result is an object filled with the first item of my array. It looks like it only works when it's located at the root content folder.
I can't find any explanation in the docs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个简单的
/pages/index.vue
文件,其中包含 2 个用例我的项目结构

nice.json
places.json5
yolo.json5
案例 2️⃣ 不适用于
.json5
文件,不确定它是否需要任何类型的特殊语法或模块来使用它。同时,案例 1️⃣ 适用于任何类型的文件(
nice.json
、places.json5
、yolo.json5
)。您还可以删除带有.where
的行以获取所有这些内容。下面的屏幕截图显示了我在示例中拥有的 5 个对象(跨所有 3 个文件)。
如此处文档中所述。
PS:如果您还想拥有
hello.md
(也在/content
目录中),您可以使用根路径,如下所示:await $content( '/', { deep: true })
您将获得hello.md
+ 5 个其他嵌套文件。Here is a simple
/pages/index.vue
file with 2 use casesMy project structure

nice.json
places.json5
yolo.json5
The case 2️⃣ is not working with a
.json5
file, not sure if it needs any kind of special syntax or a module to work with it.Meanwhile, the case 1️⃣ works totally fine with any kind of file (
nice.json
,places.json5
,yolo.json5
). And you can also remove the line with the.where
to have all of them too.Here is a screenshot showing 5 objects that I do have in my example (across all the 3 files).
As explained in the documentation here.
PS: if you want to also have
hello.md
(in the/content
directory too), you can use the root path like this:await $content('/', { deep: true })
and you'll get thehello.md
+ the 5 other nested files.