Skip to content

运行时 API 示例 ​

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

主要的 useData() API 可用于访问当前页面的站点、主题和页面数据。它同时适用于 .md 和 .vue 文件:

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

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

## 运行结果

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

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

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

### 上次更新
<pre>{{ lastUpdated }}</pre>

运行结果 ​

主题数据 ​

{
  "nav": [
    {
      "text": "首页",
      "link": "/"
    },
    {
      "text": "示例",
      "link": "/markdown-examples"
    }
  ],
  "sidebar": [
    {
      "text": "示例",
      "items": [
        {
          "text": "Markdown 示例",
          "link": "/markdown-examples"
        },
        {
          "text": "运行时 API 示例",
          "link": "/api-examples"
        }
      ]
    }
  ],
  "outline": {
    "level": [
      1,
      2
    ],
    "label": "本文目录"
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/vuejs/vitepress"
    }
  ],
  "footer": {
    "message": "Released under the MIT License.",
    "copyright": "Copyright © 2019-present Orange"
  },
  "lastUpdated": {
    "text": "上次更新"
  }
}

页面数据 ​

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

页面 Frontmatter ​

{
  "outline": "deep",
  "lastUpdated": true
}

上次更新 ​

1790761682000

更多 ​

查看文档了解运行时 API 的完整列表。

Released under the MIT License.