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 指令:
:component-example{name="ButtonExample"}
::component-example
---
name: AccordionExample
highlights: [10, 15, 20]
collapse: true
---
::
| Prop | Default | Type |
|---|---|---|
name |
| |
iframe |
Whether to render the component in an iframe | |
props |
| |
collapse |
Whether to collapse the code block
| |
options |
A list of variable props to link to the component.
| |
highlights |
A list of line numbers to highlight in the code block | |
iframeMobile |
| Whether to display the component in a mobile-sized iframe viewport |
prettier | Whether to format the code with Prettier | |
preview | Whether to show the preview
When | |
source |
| Whether to show the source code |
overflowHidden |
Whether to add overflow-hidden to wrapper |
| Slot | Type |
|---|---|
options | |
code |
|