nuxt asyncdata返回带有NUXT内容的JSON5文件的空数组

发布于 2025-01-20 06:19:40 字数 490 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许久 2025-01-27 06:19:40

这是一个简单的 /pages/index.vue 文件,其中包含 2 个用例

<template>
  <pre>{{ nuxtContent }}</pre>
</template>

<script>
export default {
  async asyncData({ $content }) {
    return {
      1️⃣
      // nuxtContent: await $content('testfoobar', { deep: true })
      //   .where({ slug: { $contains: 'yolo' } })
      //   .fetch(),

      2️⃣
      nuxtContent: await $content('testfoobar', 'nice').fetch(),
    }
  },
}
</script>

我的项目结构
输入图片此处描述

nice.json

{
  "name": "bob",
  "age": 25,
  "city": "New York"
}

places.json5

[
  {
    id: 1
  },
  {
    id: 21
  }
]

yolo.json5

[
  {
    id: 777
  },
  {
    id: 999
  }
]

案例 2️⃣ 不适用于.json5 文件,不确定它是否需要任何类型的特殊语法或模块来使用它。
同时,案例 1️⃣ 适用于任何类型的文件(nice.jsonplaces.json5yolo.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 cases

<template>
  <pre>{{ nuxtContent }}</pre>
</template>

<script>
export default {
  async asyncData({ $content }) {
    return {
      1️⃣
      // nuxtContent: await $content('testfoobar', { deep: true })
      //   .where({ slug: { $contains: 'yolo' } })
      //   .fetch(),

      2️⃣
      nuxtContent: await $content('testfoobar', 'nice').fetch(),
    }
  },
}
</script>

My project structure
enter image description here

nice.json

{
  "name": "bob",
  "age": 25,
  "city": "New York"
}

places.json5

[
  {
    id: 1
  },
  {
    id: 21
  }
]

yolo.json5

[
  {
    id: 777
  },
  {
    id: 999
  }
]

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).

enter image description here

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 the hello.md + the 5 other nested files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文