Skip to content

运行时 API 示例

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

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

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

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

## 结果

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

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

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

结果

主题数据

{
  "siteTitle": "TypeScript 中文文档",
  "nav": [
    {
      "text": "首页",
      "link": "/"
    },
    {
      "text": "入门指南",
      "link": "/getting-started/installation"
    },
    {
      "text": "类型系统",
      "link": "/type-system/basic-types"
    },
    {
      "text": "高级类型",
      "link": "/advanced-types/generics"
    },
    {
      "text": "函数",
      "link": "/functions/function-types"
    },
    {
      "text": "类",
      "link": "/classes/class-basics"
    },
    {
      "text": "枚举",
      "link": "/enums/enum-basics"
    },
    {
      "text": "模块",
      "link": "/modules/module-system"
    },
    {
      "text": "工具类型",
      "link": "/utility-types/built-in-utilities"
    },
    {
      "text": "装饰器",
      "link": "/decorators/decorator-basics"
    },
    {
      "text": "配置",
      "link": "/configuration/tsconfig"
    },
    {
      "text": "实践",
      "link": "/practices/best-practices"
    },
    {
      "text": "API 示例",
      "link": "/api-examples"
    }
  ],
  "sidebar": {
    "/getting-started/": [
      {
        "text": "入门指南",
        "items": [
          {
            "text": "为什么选择 TypeScript",
            "link": "/getting-started/why-typescript"
          },
          {
            "text": "安装",
            "link": "/getting-started/installation"
          },
          {
            "text": "快速开始",
            "link": "/getting-started/quick-start"
          }
        ]
      }
    ],
    "/type-system/": [
      {
        "text": "类型系统",
        "items": [
          {
            "text": "基础类型",
            "link": "/type-system/basic-types"
          },
          {
            "text": "对象类型",
            "link": "/type-system/object-types"
          },
          {
            "text": "数组和元组",
            "link": "/type-system/arrays-tuples"
          },
          {
            "text": "字面量类型",
            "link": "/type-system/literal-types"
          },
          {
            "text": "接口",
            "link": "/type-system/interfaces"
          },
          {
            "text": "类型别名",
            "link": "/type-system/type-aliases"
          },
          {
            "text": "联合类型",
            "link": "/type-system/union-types"
          },
          {
            "text": "交叉类型",
            "link": "/type-system/intersection-types"
          },
          {
            "text": "类型断言",
            "link": "/type-system/type-assertions"
          },
          {
            "text": "类型守卫",
            "link": "/type-system/type-guards"
          },
          {
            "text": "类型系统机制",
            "link": "/type-system/type-system-mechanics"
          }
        ]
      }
    ],
    "/advanced-types/": [
      {
        "text": "高级类型",
        "items": [
          {
            "text": "泛型",
            "link": "/advanced-types/generics"
          },
          {
            "text": "泛型约束",
            "link": "/advanced-types/generic-constraints"
          },
          {
            "text": "条件类型",
            "link": "/advanced-types/conditional-types"
          },
          {
            "text": "映射类型",
            "link": "/advanced-types/mapped-types"
          },
          {
            "text": "模板字面量类型",
            "link": "/advanced-types/template-literal-types"
          },
          {
            "text": "索引访问类型",
            "link": "/advanced-types/indexed-access-types"
          },
          {
            "text": "keyof 和 typeof 操作符",
            "link": "/advanced-types/keyof-typeof"
          },
          {
            "text": "infer 操作符",
            "link": "/advanced-types/infer-operator"
          },
          {
            "text": "递归类型",
            "link": "/advanced-types/recursive-types"
          }
        ]
      }
    ],
    "/functions/": [
      {
        "text": "函数",
        "items": [
          {
            "text": "函数类型",
            "link": "/functions/function-types"
          },
          {
            "text": "函数重载",
            "link": "/functions/function-overloads"
          },
          {
            "text": "this 类型",
            "link": "/functions/this-types"
          },
          {
            "text": "异步函数",
            "link": "/functions/async-functions"
          }
        ]
      }
    ],
    "/classes/": [
      {
        "text": "类",
        "items": [
          {
            "text": "类基础",
            "link": "/classes/class-basics"
          },
          {
            "text": "类继承",
            "link": "/classes/inheritance"
          },
          {
            "text": "访问修饰符",
            "link": "/classes/access-modifiers"
          },
          {
            "text": "抽象类",
            "link": "/classes/abstract-classes"
          },
          {
            "text": "静态成员",
            "link": "/classes/static-members"
          },
          {
            "text": "只读属性",
            "link": "/classes/readonly-properties"
          },
          {
            "text": "私有字段",
            "link": "/classes/private-fields"
          },
          {
            "text": "访问器(Getters 和 Setters)",
            "link": "/classes/getters-setters"
          }
        ]
      }
    ],
    "/enums/": [
      {
        "text": "枚举",
        "items": [
          {
            "text": "枚举基础",
            "link": "/enums/enum-basics"
          },
          {
            "text": "数字枚举",
            "link": "/enums/numeric-enums"
          },
          {
            "text": "字符串枚举",
            "link": "/enums/string-enums"
          },
          {
            "text": "常量枚举",
            "link": "/enums/const-enums"
          }
        ]
      }
    ],
    "/modules/": [
      {
        "text": "模块",
        "items": [
          {
            "text": "模块系统概述",
            "link": "/modules/module-system"
          },
          {
            "text": "导入和导出",
            "link": "/modules/import-export"
          },
          {
            "text": "模块解析策略",
            "link": "/modules/module-resolution"
          },
          {
            "text": "命名空间",
            "link": "/modules/namespaces"
          },
          {
            "text": "声明合并",
            "link": "/modules/declaration-merging"
          }
        ]
      }
    ],
    "/configuration/": [
      {
        "text": "配置",
        "items": [
          {
            "text": "tsconfig.json 配置文件",
            "link": "/configuration/tsconfig"
          },
          {
            "text": "编译选项详解",
            "link": "/configuration/compiler-options"
          },
          {
            "text": "严格模式配置",
            "link": "/configuration/strict-mode"
          },
          {
            "text": "声明文件",
            "link": "/configuration/declaration-files"
          }
        ]
      }
    ],
    "/utility-types/": [
      {
        "text": "工具类型",
        "items": [
          {
            "text": "内置工具类型",
            "link": "/utility-types/built-in-utilities"
          },
          {
            "text": "自定义工具类型",
            "link": "/utility-types/custom-utilities"
          },
          {
            "text": "工具类型模式和实践",
            "link": "/utility-types/utility-patterns"
          }
        ]
      }
    ],
    "/decorators/": [
      {
        "text": "装饰器",
        "items": [
          {
            "text": "装饰器基础",
            "link": "/decorators/decorator-basics"
          },
          {
            "text": "类装饰器详解",
            "link": "/decorators/class-decorators"
          },
          {
            "text": "装饰器工厂与元数据",
            "link": "/decorators/decorator-factories"
          }
        ]
      }
    ],
    "/practices/": [
      {
        "text": "实践",
        "items": [
          {
            "text": "最佳实践",
            "link": "/practices/best-practices"
          },
          {
            "text": "常见错误和解决方案",
            "link": "/practices/common-errors"
          },
          {
            "text": "从 JavaScript 迁移到 TypeScript",
            "link": "/practices/migration-guide"
          },
          {
            "text": "类型调试技巧",
            "link": "/practices/debugging-types"
          },
          {
            "text": "性能优化建议",
            "link": "/practices/performance"
          }
        ]
      }
    ]
  },
  "socialLinks": [
    {
      "icon": "github",
      "link": "https://github.com"
    }
  ],
  "footer": {
    "message": "基于 VitePress 构建",
    "copyright": "Copyright © 2026 TypeScript 中文文档"
  },
  "search": {
    "provider": "local"
  },
  "editLink": {
    "pattern": "",
    "text": "在 GitHub 上编辑此页"
  },
  "lastUpdated": {
    "text": "最后更新于",
    "formatOptions": {
      "dateStyle": "short",
      "timeStyle": "medium"
    }
  },
  "outline": {
    "level": [
      2,
      3
    ],
    "label": "页面导航"
  }
}

页面数据

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

页面 Frontmatter

{
  "outline": "deep"
}

更多

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

最后更新于:

基于 VitePress 构建