运行时 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>Results
Theme Data
{
"nav": [
{
"text": "首页",
"link": "/zh/"
},
{
"text": "示例",
"link": "/zh/markdown-examples"
}
],
"sidebar": [
{
"text": "示例",
"items": [
{
"text": "Markdown 示例",
"link": "/zh/markdown-examples"
},
{
"text": "运行时 API 示例",
"link": "/zh/api-examples"
}
]
}
],
"socialLinks": [
{
"icon": "github",
"link": "https://github.com/vuejs/vitepress"
}
]
}Page Data
{
"title": "运行时 API 示例",
"description": "",
"frontmatter": {
"outline": "deep"
},
"headers": [],
"relativePath": "zh/api-examples.md",
"filePath": "zh/api-examples.md"
}Page Frontmatter
{
"outline": "deep"
}More
Check out the documentation for the full list of runtime APIs.