运行时 API 示例
本页演示了 VitePress 提供的一些运行时 API 的用法。
主的 useData()
API 可用于访问当前页面的站点、主题和页面数据。它适用于 .md
和 .vue
文件:
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>
结果
主题数据
{ "nav": [ { "text": "首页", "link": "/zh-cn" }, { "text": "示例", "link": "/zh-cn/markdown-examples" } ], "sidebar": [ { "text": "示例", "items": [ { "text": "Markdown 示例", "link": "/zh-cn/markdown-examples" }, { "text": "运行时 API 示例", "link": "/zh-cn/api-examples" } ] } ], "docFooter": { "prev": "上一页", "next": "下一页" }, "outline": { "label": "页面导航" }, "lastUpdated": { "text": "最后更新于" }, "notFound": { "title": "页面未找到", "quote": "但如果你不改变方向,并且继续寻找,你可能最终会到达你所前往的地方。", "linkLabel": "前往首页", "linkText": "带我回首页" }, "langMenuLabel": "多语言", "returnToTopLabel": "回到顶部", "sidebarMenuLabel": "菜单", "darkModeSwitchLabel": "主题", "lightModeSwitchTitle": "切换到浅色模式", "darkModeSwitchTitle": "切换到深色模式", "skipToContentLabel": "跳转到内容", "logo": "./favicon.svg", "socialLinks": [ { "icon": "github", "link": "https://github.com/JustElaina/docs" } ] }
页面数据
{ "title": "运行时 API 示例", "description": "", "frontmatter": { "outline": "deep" }, "headers": [], "relativePath": "zh-cn/api-examples.md", "filePath": "zh-cn/api-examples.md", "lastUpdated": 1754906484000 }
页面 Frontmatter
{ "outline": "deep" }
更多
请查看运行时 API 完整列表的文档。