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

Terminal
npx nuxt module add content

创建内容

将你的 Markdown 文件放到 content/ 目录中:

content/index.md
# Hello Content

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

渲染内容

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

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://nuxt-content.zhcndoc.com 了解更多关于 Content 模块的功能,例如如何构建查询以及如何在 Markdown 文件中使用 MDC 语法嵌入 Vue 组件。