content

使用 content/ 目录为您的应用程序创建一个基于文件的 CMS。

Nuxt Content 会读取您项目中的 content/ 目录 并解析 .md.yml.csv.json 文件,以创建一个基于文件的 CMS。

  • 使用内置组件渲染您的内容。
  • 使用类似 MongoDB 的 API 查询您的内容。
  • 在 Markdown 文件中使用 MDC 语法使用您的 Vue 组件。
  • 自动生成您的导航。
Nuxt Content 文档中了解更多信息。

启用 Nuxt Content

通过一个命令在您的项目中安装 @nuxt/content 模块并将其添加到 nuxt.config.ts

终端
npx nuxi module add content

创建内容

将您的 markdown 文件放置在 content/ 目录中:

content/index.md
# 你好,内容

该模块会自动加载并解析它们。

渲染内容

要渲染内容页面,请使用 <ContentRenderer> 组件添加一个 catch-all 路由

pages/[...slug].vue
<script lang="ts" setup>
const route = useRoute()
const { data: page } = await useAsyncData(route.path, () => {
  return queryCollection('content').path(route.path).first()
})
</script>

<template>
  <div>
    <header><!-- ... --></header>

    <ContentRenderer v-if="page" :value="page" />

    <footer><!-- ... --></footer>
  </div>
</template>

文档

前往 https://content.nuxt.com 了解有关内容模块功能的更多信息,例如如何构建查询以及如何在 Markdown 文件中使用 Vue 组件的 MDC 语法。