组件开发工具
本目录包含用于开发 Vue 组件的通用工具函数、类型定义和样式变量。
目录结构
docs/.vitepress/theme/
├── composables/
│ └── useAnimation.ts # 动画工具 Composable
├── types/
│ └── components.ts # 通用类型定义
└── styles/
└── variables.css # 样式变量和混入使用指南
1. 动画工具 (useAnimation)
提供定时器管理和常用动画函数,自动清理定时器避免内存泄漏。
vue
可用函数:
delay(ms)- 延迟执行(自动管理定时器)setTimeout(callback, ms)- 创建受管理的 setTimeoutsetInterval(callback, ms)- 创建受管理的 setIntervalclearTimeout(id)- 清除特定的 timeoutclearInterval(id)- 清除特定的 intervalclearAll()- 清除所有定时器typeText(targetText, speed, onUpdate)- 打字机效果animatePacket(duration, onUpdate)- 数据包移动动画playSteps(steps, stepDelay, onStepChange)- 步骤导航动画fade(element, duration, fadeIn)- 淡入淡出动画
2. 类型定义 (components.ts)
提供常用的组件类型定义。
vue
可用类型:
Step- 步骤定义AnimationPhase- 动画阶段ComparisonItem- 对比项FlowNode- 流程节点FlowConnection- 流程连接HierarchyNode- 层级节点MatrixCell- 矩阵单元格CardItem- 卡片项TimelineEvent- 时序事件NetworkLayer- 网络层级TestScenario- 测试场景AuthMethod- 认证方法DeploymentMethod- 部署方式Priority- 优先级Status- 状态
3. 样式变量 (variables.css)
提供统一的样式变量和工具类。
vue
可用变量:
颜色:
--color-success- 成功色 (#15a051)--color-info- 信息色 (#2eb3df)--color-warning- 警告色 (#D4952C)--color-error- 错误色 (#ff3b30)--color-primary- 主色 (#007aff)
间距:
--spacing-xs- 4px--spacing-sm- 8px--spacing-md- 16px--spacing-lg- 24px--spacing-xl- 32px
圆角:
--radius-sm- 4px--radius-md- 8px--radius-lg- 12px--radius-xl- 16px--radius-full- 9999px
阴影:
--shadow-sm- 小阴影--shadow-md- 中阴影--shadow-lg- 大阴影--shadow-xl- 超大阴影
工具类:
.component-wrapper- 组件容器.component-header- 组件头部.component-title- 组件标题.component-body- 组件主体.btn- 按钮基础样式.btn-primary- 主按钮样式.card- 卡片样式.badge- 徽章样式.badge-success/info/warning/error- 不同状态的徽章
动画:
@keyframes fadeIn- 淡入动画@keyframes slideIn- 滑入动画@keyframes pulse- 脉冲动画@keyframes spin- 旋转动画
组件开发模板
vue
<template>
<div class="component-wrapper">
<div class="component-header">
<h3 class="component-title">组件标题</h3>
<button class="btn btn-primary" @click="startAnimation" :disabled="isAnimating">
{{ isAnimating ? '播放中...' : '播放' }}
</button>
</div>
<div class="component-body">
</div>
</div>
</template>最佳实践
- 定时器管理:始终使用
useAnimation提供的定时器函数,确保组件卸载时自动清理 - 类型安全:使用 TypeScript 类型定义,提高代码可维护性
- 样式一致性:使用统一的样式变量,确保组件风格一致
- 响应式设计:所有组件都应支持移动端(640px 断点)
- 深色模式:使用 VitePress 主题变量,自动适配深色模式
- 性能优化:避免不必要的重渲染,使用
computed缓存计算结果
注意事项
- 所有定时器都会在组件卸载时自动清理,无需手动调用
onUnmounted - 样式变量会自动适配深色模式,无需额外处理
- 响应式断点统一使用 640px(移动端)
- 动画时长建议:快速 150ms,基础 200ms,慢速 300ms