Skip to content

运行时 API 示例

本页演示 VitePress 提供的部分运行时 API 的使用方式。

主要的 useData() API 可用于获取当前页面的站点、主题和页面数据。该 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": "主页",
      "link": "/"
    },
    {
      "text": "示例",
      "link": "/examples/markdown-examples"
    }
  ],
  "sidebar": [
    {
      "text": "示例",
      "items": [
        {
          "text": "Markdown 示例",
          "link": "/examples/markdown-examples"
        },
        {
          "text": "运行时 API 示例",
          "link": "/examples/api-examples"
        }
      ]
    }
  ],
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/potatoone"
    }
  ]
}

页面数据

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

页面 Frontmatter

{
  "outline": "deep"
}

更多

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