Skip to content

运行时 API 示例

本页面演示了 VitePress 提供的一些运行时 API 的用法。

主要的 useData() API 可用于访问当前页面的站点、主题和页面数据。它在 .md.vue 文件中都可以使用:

md
<script setup>
import { useData } from 'vitepress'

const { theme, page, frontmatter } = useData()
</script>

## 结果

### 主题数据
<pre>{{ theme }}</pre>

### 页面数据
<pre>{{ page }}</pre>

### 页面 Frontmatter
<pre>{{ frontmatter }}</pre>

结果

主题数据

{
  "nav": [
    {
      "text": "Home",
      "link": "/"
    },
    {
      "text": "Examples",
      "link": "/markdown-examples"
    },
    {
      "text": "Vuetify 集成",
      "link": "/vuetify-integration"
    },
    {
      "text": "故障排除",
      "link": "/troubleshooting"
    }
  ],
  "sidebar": [
    {
      "text": "Examples",
      "items": [
        {
          "text": "Markdown Examples",
          "link": "/markdown-examples"
        },
        {
          "text": "Runtime API Examples",
          "link": "/api-examples"
        }
      ]
    },
    {
      "text": "开发指南",
      "items": [
        {
          "text": "Vuetify 集成",
          "link": "/vuetify-integration"
        },
        {
          "text": "故障排除",
          "link": "/troubleshooting"
        }
      ]
    }
  ],
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/vuejs/vitepress"
    }
  ]
}

页面数据

{
  "title": "运行时 API 示例",
  "description": "",
  "frontmatter": {
    "outline": "deep"
  },
  "headers": [],
  "relativePath": "api-examples.md",
  "filePath": "api-examples.md"
}

页面 Frontmatter

{
  "outline": "deep"
}

更多

查看文档以获取完整的运行时 API 列表