Skip to content

运行时 API 示例

This page demonstrates usage of some of the runtime APIs provided by VitePress.

The main useData() API can be used to access site, theme, and page data for the current page. It works in both .md and .vue files:

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

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

## Results

### Theme Data
<pre>{{ theme }}</pre>

### Page Data
<pre>{{ page }}</pre>

### Page Frontmatter
<pre>{{ frontmatter }}</pre>

Results

Theme Data

{
  "nav": [
    {
      "text": "主页",
      "link": "/"
    }
  ],
  "sidebar": {
    "/": [
      {
        "text": "回到主页",
        "link": "/"
      },
      {
        "text": "示例文档",
        "items": [
          {
            "text": "Markdown 示例",
            "link": "/markdown-examples"
          },
          {
            "text": "API 示例",
            "link": "/api-examples"
          }
        ]
      },
      {
        "text": "外链示例",
        "link": "https://not-exist.wss.moe/outlink-zh"
      }
    ],
    "/en_us/": [
      {
        "text": "Back to homepage",
        "link": "/en_us/"
      },
      {
        "text": "Example docs",
        "items": [
          {
            "text": "Markdown Examples",
            "link": "/en_us/markdown-examples"
          },
          {
            "text": "API Examples",
            "link": "/en_us/api-examples"
          }
        ]
      },
      {
        "text": "Outlink Test",
        "link": "https://not-exist.wss.moe/outlink-en"
      }
    ]
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/vuejs/vitepress"
    },
    {
      "icon": "qq",
      "link": "https://not-exist.wss.moe/qq"
    },
    {
      "icon": "discord",
      "link": "https://not-exist.wss.moe/discord"
    }
  ],
  "editLink": {
    "pattern": "https://github.com/wyf9/vitepress-template/edit/main/:path",
    "text": "在 GitHub 上编辑本页"
  },
  "lastUpdated": {
    "text": "最后更新于",
    "formatOptions": {
      "dateStyle": "full",
      "timeStyle": "full"
    }
  }
}

Page Data

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

Page Frontmatter

{
  "outline": "deep"
}

More

Check out the documentation for the full list of runtime APIs.