viepress:vue组件展示和源码功能

VitePress实战:vue组件展示和源码功能

一、项目结构与基础配置

1.1 侧边栏配置(sidebar.ts

typescript 复制代码
// docs/.vitepress/sidebar.ts
export default {
  '/example/': [
    {
      text: 'Examples',
      items: [
        { text: 'Markdown Examples', link: '/example/markdown' },
        { text: 'Runtime API Examples', link: '/example/api' }
      ]
    }
  ],
  '/guide/': [
    {
      text: '组件',
      items: [
        { text: '引入', link: '/guide/imp' },
        { text: '使用', link: '/guide/use' },
        { text: '预览', link: '/guide/preview' }
      ]
    }
  ]
}
  • 侧边栏分为Examples组件两部分,通过路径 /example//guide/ 分层管理文档
  • 组件文档包含 引入 (基础用法)、使用 (高级特性)、预览(交互演示)三级结构

1.2 导航栏配置(nav.ts

typescript 复制代码
// docs/.vitepress/nav.ts
export const nav = [
  { text: 'Home', link: '/' },
  { text: 'Examples', link: '/example/markdown' },
  { text: '组件', link: '/guide/imp' }
]
  • 导航栏直接指向组件文档入口 /guide/imp,体现组件功能的核心地位
  • 示例页面通过二级链接 /example/markdown 进行扩展

此篇文章重点梳理的是vue组件展示和源码功能,其基础配置可查看从入门到进阶:VitePress 构建高效静态站点全指南

二、核心插件集成

2.1 预览插件配置(config.mts

typescript 复制代码
// docs/.vitepress/config.mts
import { componentPreview,containerPreview } from '@vitepress-demo-preview/plugin'
import { codePreviewPlugin } from 'vitepress-script-preview'

export default defineConfig({
  markdown: {
    config(md) {
      // 组件预览插件
      md.use(componentPreview, { clientOnly: true })
      // 代码容器预览
      md.use(containerPreview,{ clientOnly: true })
      md.use(codePreviewPlugin, { clientOnly: true })
    }
  }
})

2.2 主题配置(theme.mts`)

typescript 复制代码
// docs/.vitepress/theme/index.ts

import { h } from 'vue'
import type { Theme } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import 'vitepress-script-preview/components/style.css'
import './style.css'
import { CodePreview } from 'vitepress-script-preview/components';
import '@vitepress-demo-preview/component/dist/style.css'
import { ElementPlusContainer } from '@vitepress-demo-preview/component'
export default {
  extends: DefaultTheme,
  Layout: () => {
    return h(DefaultTheme.Layout, null, {
      // https://vitepress.dev/guide/extending-default-theme#layout-slots
    })
  },
  enhanceApp({ app, router, siteData }) {
    app.component('CodePreview', CodePreview);
    app.component('demo-preview', ElementPlusContainer)
    // ...
  }
} satisfies Theme

三、组件预览实战

3.1 基础用法示例

在Markdown中使用 :::preview 块实现组件交互:

markdown 复制代码
<!-- docs/guide/preview.md -->
## 组件预览功能

```css
:::preview
demo-preveiw="./xx.vue"
:::

四. 总结

功能库:@vitepress-demo-preview/component、@vitepress-demo-preview/plugin、vitepress-script-preview

相关推荐
老王以为5 分钟前
走进 AI Agent 第四篇(上):知识获取管道——RAG 基础
前端·人工智能·全栈
夏天要喝冰可乐18 分钟前
一处写作,多平台分发:我给掘金做了一款开源 Chrome 插件
前端·chrome·vibecoding
老男孩游戏时光22 分钟前
Vue3 Composition API 最佳实践,别再乱写 setup 了
vue.js
+VX:Fegn089524 分钟前
计算机毕业设计|基于java+ vue共享单车信息系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
货拉拉技术29 分钟前
货拉拉开源Hadice:安卓 & 鸿蒙桌面调试工具
前端
程序猿DD1 小时前
OctaFuse Gateway 2.11.0:流式请求优化、用户折扣策略与错误契约升级
前端·后端
尾善爱看海1 小时前
《JavaScript 数组操作全攻略:12 类 API + 30 个实战场景 + 20 个避坑指南》
前端·javascript·面试
Bs_MoneyMagnet1 小时前
基于springboot+vue的非遗物质文化遗产系统的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
计算机毕设定制辅导-无忧学长1 小时前
《基于SpringBoot的未成年人健康知识科普平台的设计与实现》
java·开发语言·vue.js·spring boot·未成年人健康知识科普平台
计算机魔术师1 小时前
Anthropic 估值冲 4 万亿,但企业客户已经偷偷换了便宜模型
前端