帮助开发者掌握组件封装方法,优化重复性组件与布局,使代码简洁易维护。示例涵盖组件公共样式、自定义组件、弹窗及全局@Builder等封装实现,通过具体场景展示封装技巧。
| Files | Last commit | Last update |
|---|---|---|
| 1 year ago | ||
| 7 months ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 2 months ago | ||
| 2 months ago | ||
| 1 year ago | ||
| 1 year ago | ||
| 1 year ago |
ArkUI Component Encapsulation
Overview
This sample describes three typical scenarios of ArkUI component encapsulation: common component encapsulation, dialog component encapsulation, and component factory encapsulation.
Preview
| Home page | Attribute Style | Common component | Dialog component | Component factory |
|---|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
How to Use
On the home page, tap the corresponding button to access the page for common component encapsulation, dialog component encapsulation, or component factory encapsulation.
Project Directory
├──entry/src/main/ets/
│ ├──common
│ │ └──CommonConstants.ets // Common constants
│ ├──entryability
│ │ └──EntryAbility.ets // Entry ability
│ ├──model
│ │ ├──AttributeModifier.ets // AttributeModifier interface
│ │ ├──GetResourceString.ets // Resource-to-string conversion function
│ │ └──PopViewUtils.ets // Custom pop-up window
│ ├──pages
│ │ ├──AttributeStylePage.ets // Attribute Style
│ │ ├──CommonComponent.ets // Common component
│ │ ├──ComponentFactory.ets // Component factory
│ │ ├──DialogComponent.ets // Dialog component
│ │ └──Index.ets // Home page
│ └──view
│ ├──CustomImageText.ets // Custom image and text component
│ └──FactoryMap.ets // Component factory
└──entry/src/main/resources // Static resources of the app
How to Implement
-
Common component encapsulation: The attributeModifier method is provided, and the AttributeModifier API is implemented through the custom class to extend the system component attributes.
-
Dialog component encapsulation: The PromptAction object obtained from UIContext is used to encapsulate the custom dialog box. The openCustomDialog API in the PromptAction object is used to open the dialog box, and the closeCustomDialog API is used to close the dialog box.
-
Component factory encapsulation: The Map structure is used to store encapsulated components. The provider encapsulates the factory components using the global @Builder method, wraps the global @Builder method using the wrapBuilder function, and stores the return as the value of the component factory Map. The corresponding component is obtained based on the key value of Map.
Permissions
N/A
Constraints
-
The sample is supported bar-phone with standard systems.
-
The HarmonyOS version must be HarmonyOS 5.0.5 Release or later.
-
The DevEco Studio version must be DevEco Studio HarmonyOS 5.0.5 Release or later.
-
The HarmonyOS SDK version must be HarmonyOS 5.0.5 Release SDK or later.
Introduction
帮助开发者掌握组件封装方法,优化重复性组件与布局,使代码简洁易维护。示例涵盖组件公共样式、自定义组件、弹窗及全局@Builder等封装实现,通过具体场景展示封装技巧。
Customize your domain



