# 组件 组件是PE引擎中可复用的UI构建块,它们将模板、样式和逻辑封装在一起,使得代码更加模块化和可维护。 ## 什么是组件? 组件是PE应用中独立的、可复用的代码单元,它包含: 1. **模板** - 定义组件的结构和元素 2. **样式** - 定义组件的外观和样式 3. **逻辑** - 定义组件的行为和功能 ## 创建组件 在PE中,组件可以通过创建独立的目录结构来定义: ``` components/ ├── button/ │ ├── index.pe │ └── index.less ├── card/ │ ├── index.pe │ └── index.less └── modal/ ├── index.pe └── index.less ``` ### 组件模板文件 组件模板文件与场景模板文件结构相同: ```html {{ label }} ``` ### 组件样式文件 ```less /* components/button/index.less */ .pe-button { display: inline-flex; align-items: center; justify-content: center; padding: 10px 20px; background-color: #3498db; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: all 0.3s ease; &:hover { background-color: #2980b9; } &:active { transform: translateY(1px); } .button-text { margin: 0; font-weight: normal; } } ``` ## 使用组件 在场景中使用组件非常简单: ```html ``` ## 组件通信 组件之间可以通过属性和事件进行通信。 ### Props(属性) 父组件向子组件传递数据: ```html {{ name }} 年龄: {{ age }} 邮箱: {{ email }} 编辑 ``` ### Events(事件) 子组件向父组件传递数据: ```html ``` ## 插槽(Slots) 插槽允许父组件向子组件传递内容: ```html {{ title }} ``` ```html 这是卡片的主要内容 ``` ## 动态组件 PE支持动态组件,可以根据条件渲染不同的组件: ```html 显示按钮 显示卡片 ``` ## 组件生命周期 组件也有自己的生命周期钩子,与场景生命周期类似: ```html 组件内容 ``` ## 组件最佳实践 ### 1. 单一职责原则 每个组件应该只负责一个功能: ```html ``` ### 2. 合理的组件结构 ```html {{ name }} ¥{{ price }} ``` ### 3. 组件样式隔离 ```less /* components/product-card/index.less */ .product-card { width: 300px; border: 1px solid #ddd; border-radius: 8px; overflow: hidden; background: white; box-shadow: 0 2px 8px rgba(0,0,0,0.1); .product-image { width: 100%; height: 200px; background-color: #f5f5f5; } .product-info { padding: 15px; .product-name { font-size: 18px; font-weight: bold; margin-bottom: 10px; } .product-price { color: #e74c3c; font-size: 16px; font-weight: bold; margin-bottom: 15px; } .product-actions { display: flex; gap: 10px; .button { flex: 1; } } } } ``` ## 完整示例 以下是一个完整的组件示例: ### components/modal/index.pe ```html {{ title }} ``` ### components/modal/index.less ```less .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: none; align-items: center; justify-content: center; z-index: 1000; } .modal-container { background: white; border-radius: 8px; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3); min-width: 300px; max-width: 500px; max-height: 80vh; overflow: hidden; display: flex; flex-direction: column; } .modal-header { padding: 15px 20px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; .modal-title { font-size: 18px; font-weight: bold; margin: 0; } .modal-close { cursor: pointer; font-size: 20px; width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; border-radius: 50%; &:hover { background-color: #f5f5f5; } } } .modal-body { padding: 20px; flex: 1; overflow-y: auto; } .modal-footer { padding: 15px 20px; border-top: 1px solid #eee; display: flex; justify-content: flex-end; gap: 10px; .btn { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; &.btn-primary { background-color: #3498db; color: white; &:hover { background-color: #2980b9; } } &.btn-secondary { background-color: #95a5a6; color: white; &:hover { background-color: #7f8c8d; } } } } ``` ### 使用模态框组件 ```html 您确定要执行此操作吗? ``` 通过以上内容,你已经了解了PE引擎的组件系统。组件是构建复杂应用的基础,合理使用组件可以大大提高代码的可维护性和复用性。