ComponentExample

学习如何使用 ComponentExample 组件创建引人入胜的交互式组件示例,功能包括实时预览、源代码展示、代码高亮和折叠,以极大地提升文档的交互性和用户体验。

用法

ComponentExample 用于在文档中嵌入可交互的组件示例。它会自动加载组件,并展示实时预览和源代码。

使用 ComponentExample 前,需要在 nuxt.config.ts 中配置示例组件目录,以确保组件被全局注册:
import { createResolver } from '@nuxt/kit'

const { resolve } = createResolver(import.meta.url)

export default defineNuxtConfig({
  modules: [
    (_, nuxt) => {
      nuxt.hook('components:dirs', (dirs) => {
        dirs.unshift({ 
          path: resolve('./app/components/content/examples'), 
          pathPrefix: false, 
          prefix: '', 
          global: true 
        })
      })
    }
  ]
})
此配置将 ./app/components/content/examples 目录下的所有组件注册为全局组件,使 ComponentExample 能够动态加载它们。

基本用法

在您的 Markdown 文档中使用 ::component-example 指令:

md
:component-example{name="ButtonExample"}

高级配置

md
::component-example
---
name: AccordionExample
highlights: [10, 15, 20]
collapse: true
---
::

API

Props

Prop Default Type
namestring
iframefalseboolean | { [key: string]: any; }

是否在 iframe 中渲染组件

props{ [key: string]: any; }
collapsefalseboolean | { icon?: string; name?: string; openText?: string; closeText?: string; open?: boolean; }

是否折叠代码块

options{ alias?: string; name: string; label: string; items?: any[]; default: any; multiple?: boolean; }[]

链接到组件的可变属性列表

highlightsnumber[]

代码块中需要高亮的行号列表

iframeMobilefalseboolean

是否在移动端尺寸的 iframe 视口中显示组件

prettierfalseboolean

是否使用 Prettier 格式化代码

previewtrueboolean

是否显示预览 当设置为 false 时,将显示文件名

sourcetrueboolean

是否显示源代码

overflowHiddenboolean

是否在包装器上添加 overflow-hidden

Slots

Slot Type
options{}
code{}

Changelog

4e45d — refactor: 重构组件结构和优化代码组织

9cfe6 — refactor: 调整 ComponentExample 默认配置

6cd2f — 💄 style: 修复组件示例中颜色选择器的样式类名

8915e — ♻️ refactor: 优化 Vue 类名绑定语法

586a5 — ✨ feat: 添加 Nuxt 文档主题 layer 核心代码

Copyright © 2024 - 2026