Skip to content

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

{
  "logo": {
    "src": "/vitepress-logo-mini.svg",
    "width": 24,
    "height": 24
  },
  "nav": [
    {
      "text": "主页",
      "link": "/"
    },
    {
      "text": "详情页",
      "link": "/javascript/class%20%E7%9A%84%20function%20%E5%92%8C%20prototype%20%E5%AE%9E%E7%8E%B0.md"
    }
  ],
  "sidebar": [
    {
      "text": "React",
      "collapsed": true,
      "items": [
        {
          "text": "useEffect 执行顺序",
          "link": "/react/useEffect%20%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F.md"
        },
        {
          "text": "实现 useHover",
          "link": "/react/%E5%AE%9E%E7%8E%B0%20useHover.md"
        },
        {
          "text": "为什么使用 useCallback",
          "link": "/react/%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BD%BF%E7%94%A8%20useCallback.md"
        }
      ]
    },
    {
      "text": "Vue",
      "collapsed": true,
      "items": [
        {
          "text": "vue_shop 的 webpack 打包策略",
          "link": "/vue/vue_shop%20%E7%9A%84%20webpack%20%E6%89%93%E5%8C%85%E7%AD%96%E7%95%A5.md"
        },
        {
          "text": "vue.config,js 配置项分析",
          "link": "/vue/vue.config,js%20%E9%85%8D%E7%BD%AE%E9%A1%B9%E5%88%86%E6%9E%90.md"
        }
      ]
    },
    {
      "text": "JavaScript",
      "collapsed": true,
      "items": [
        {
          "text": "class 的 function 和 prototype 实现",
          "link": "/javascript/class%20%E7%9A%84%20function%20%E5%92%8C%20prototype%20%E5%AE%9E%E7%8E%B0.md"
        },
        {
          "text": "vscode 的 live reload 源代码分析",
          "link": "/javascript/vscode%20%E7%9A%84%20live%20reload%20%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%90.md"
        }
      ]
    },
    {
      "text": "TypeScript",
      "collapsed": true,
      "items": [
        {
          "text": "实现 Filter<T, A>",
          "link": "/typescript/%E5%AE%9E%E7%8E%B0%20Filter.md"
        },
        {
          "text": "实现 Slice<A, S, E>",
          "link": "/typescript/%E5%AE%9E%E7%8E%B0%20Slice.md"
        }
      ]
    },
    {
      "text": "CSS",
      "collapsed": true,
      "items": [
        {
          "text": "css-in-depth 学习笔记",
          "link": "/css/css-in-depth%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0.md"
        },
        {
          "text": "twitter css 布局",
          "link": "/css/twitter%20css%20%E5%B8%83%E5%B1%80.md"
        },
        {
          "text": "background-position 的使用",
          "link": "/css/background-position%20%E7%9A%84%E4%BD%BF%E7%94%A8.md"
        }
      ]
    },
    {
      "text": "HTML",
      "collapsed": true,
      "items": [
        {
          "text": "HTML 语义化",
          "link": "/html/html%20%E8%AF%AD%E4%B9%89%E5%8C%96"
        }
      ]
    },
    {
      "text": "Markdown Examples",
      "link": "/markdown-examples"
    },
    {
      "text": "Runtime API Examples",
      "link": "/api-examples"
    }
  ],
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com/Hercules11/frontend-cheat-sheet",
      "ariaLabel": "源代码仓库"
    }
  ],
  "darkModeSwitchLabel": "主题"
}

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.