content

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

Nuxt Content 读取项目中的 content/ 目录并解析 .md.yml.csv.json 文件,为您的应用程序创建基于文件的 CMS。

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

启用 Nuxt Content

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

Terminal
npx nuxt module add content

创建内容

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

content/index.md
# Hello Content

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

渲染内容

要渲染内容页面,使用 <ContentRenderer> 组件添加一个捕获所有路由

app/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 了解有关 Content 模块功能的更多信息,例如如何构建查询以及使用 MDC 语法在 Markdown 文件中使用 Vue 组件。