Runtime API Examples
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": "Home", "link": "/" }, { "text": "Examples", "link": "/markdown-examples" } ], "sidebar": { "/base/": [ { "text": "JavaScript", "collapsed": false, "items": [ { "text": "原型链", "link": "/base/js-prototype" }, { "text": "数据类型", "link": "/base/js-type" }, { "text": "数组方法", "link": "/base/js-array" }, { "text": "对象方法", "link": "/base/js-object" }, { "text": "正则表达式", "link": "/base/js-regex" }, { "text": "面试题", "collapsed": false, "items": [ { "text": "1.如何判断元素的变化", "link": "/base/js-interview/q1" }, { "text": "2.说说你对闭包的理解", "link": "/base/js-interview/q2" }, { "text": "问题3", "link": "/base/js-interview/question3" } ] } ] }, { "text": "HTML", "collapsed": true, "items": [ { "text": "原型链", "link": "/base/" }, { "text": "数组vs对象", "link": "/base/guide" } ] }, { "text": "CSS", "collapsed": true, "items": [ { "text": "原型链", "link": "/base/" }, { "text": "数组vs对象", "link": "/base/guide" } ] } ], "/react/": [ { "text": "React 教程", "collapsed": false, "items": [ { "text": "简介", "link": "/react/" }, { "text": "Hooks", "link": "/react/hooks" }, { "text": "组件", "link": "/react/component" } ] } ], "/": [ { "text": "首页", "items": [ { "text": "开始使用", "link": "/" } ] } ] }, "socialLinks": [ { "icon": "github", "link": "https://github.com/vuejs/vitepress" } ] }
Page Data
{ "title": "Runtime API Examples", "description": "", "frontmatter": { "outline": "deep" }, "headers": [], "relativePath": "api-examples.md", "filePath": "api-examples.md" }
Page Frontmatter
{ "outline": "deep" }
More
Check out the documentation for the full list of runtime APIs.