# 组件创建指南
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/02.%20%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF/01.%20%E7%BB%84%E4%BB%B6%E5%88%9B%E5%BB%BA%E6%8C%87%E5%8D%97


# 组件创建指南

## 参考文件
- `src/views/flow/src/extensions/registerNode/tasks/crowdSelect/CrowdSelectModal.vue`
- `src/views/flow/src/extensions/registerNode/tasks/crowdSelect/CrowdSelect.ts`

## 1. 组件创建步骤

### 1.1 创建组件文件
- 在 `src/views/flow/src/extensions/registerNode/tasks/` 下创建组件目录
- 创建以下文件：
  - `组件名Modal.vue`：主组件文件
  - `组件名Data.ts`：静态数据文件（可选）
  - `组件名.ts`：组件导出文件

### 1.2 实现组件功能
- 使用 Vue 3 组合式 API (`<script setup>`)
- 使用 Ant Design Vue 组件
- 使用 VueUse 工具库
- 添加中文注释
- 使用 TypeScript 确保类型安全

### 1.3 导出组件
- 继承 `BaseCardViewModel` 和 `BaseCardView`
- 实现 `setAttributes` 和 `getDefaultAnchor` 方法
- 创建并导出 `组件名Task` 对象

## 2. 组件集成步骤

### 2.1 在 ModalSetting.vue 中集成
1. 导入组件：
```typescript
import 组件名Modal from '/@/views/flow/src/extensions/registerNode/tasks/组件目录/组件名Modal.vue';
```

2. 在 componentConfig 中添加映射：
```typescript
const componentConfig = {
  [StepTypes.组件类型]: 组件名Modal,
  // 其他组件...
};
```

### 2.2 在 constant.ts 中定义
1. 在 StepTypes 枚举中添加类型：
```typescript
export enum StepTypes {
  组件类型 = '组件类型',
  // 其他类型...
}
```

2. 在 nodeSize 中添加尺寸配置：
```typescript
export const nodeSize: Record<StepTypes, { width: number; height: number }> = {
  [StepTypes.组件类型]: {
    width: 280,
    height: 159,
  },
  // 其他配置...
};
```

3. 在 nodeIDPrefixs 中添加前缀配置：
```typescript
export const nodeIDPrefixs: Record<StepTypes, string> = {
  [StepTypes.组件类型]: 'tasks',
  // 其他配置...
};
```

### 2.3 在 index.ts 中注册
1. 导入组件：
```typescript
import 组件名 from '/@/views/flow/src/extensions/registerNode/tasks/组件目录/组件名';
```

2. 在 FlowElement 中注册：
```typescript
lf.register(组件名);
```

## 3. 文件结构说明

```
src/views/flow/src/extensions/registerNode/tasks/
└── 组件名/
    ├── 组件名Modal.vue      // 主组件
    ├── 组件名Data.ts        // 静态数据
    └── 组件名.ts            // 组件导出
```

## 4. 注意事项

1. 组件命名规范：
   - 目录名：小写字母，使用连字符连接
   - 类名：大驼峰命名
   - 文件名：与类名一致

2. 类型安全：
   - 使用 TypeScript 类型定义
   - 为所有 props 和 emits 定义类型
   - 使用泛型定义 useStorage 等工具

3. 样式规范：
   - 使用 scoped CSS
   - 遵循 BEM 命名规范
   - 使用 Ant Design 的样式变量

4. 代码组织：
   - 逻辑代码按功能模块划分
   - 使用组合式 API 的 setup 函数
   - 保持组件单一职责

5. 尽量和参考组件一致
   - 样式尽量保持一致
   - 方法尽量保持一致，不要丢失参考项目中的方法，比如初始化init等，这些方法会被父组件使用！
