文档管理中心
我的
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
问题处理与运维开发与测试问题应用框架开发UI框架组件使用常见问题如何封装Popup组件到SDK

如何封装Popup组件到SDK

Q:如何将Popup组件封装到har包中,以便在项目中统一使用。

A:可以使用openPopup方法为指定组件弹出气泡。示例代码如下:

  1. har包封装:
    收起
    自动换行
    深色代码主题
    复制
    1. import { BusinessError } from '@kit.BasicServicesKit';
    2. import { ComponentContent, TargetInfo, PromptAction } from '@kit.ArkUI';
    3. export class PromptActionClass {
    4. private promptAction: PromptAction | null = null;
    5. private contentNode: ComponentContent<Object> | null = null; // popup弹框组件内容
    6. private options: PopupCommonOptions | null = null;
    7. private target: TargetInfo | null = null; // 目标组件信息
    8. private isPartialUpdate: boolean = false;
    9. public setPromptAction(promptAction: PromptAction) {
    10. this.promptAction = promptAction;
    11. }
    12. public setContentNode(node: ComponentContent<Object>) {
    13. this.contentNode = node;
    14. }
    15. public setTarget(target: TargetInfo) {
    16. this.target = target;
    17. }
    18. public setOptions(options: PopupCommonOptions) {
    19. this.options = options;
    20. }
    21. public setIsPartialUpdate(isPartialUpdate: boolean) {
    22. this.isPartialUpdate = isPartialUpdate;
    23. }
    24. // 打开popup
    25. public openPopup() {
    26. if (this.promptAction != null) {
    27. this.promptAction.openPopup(this.contentNode, this.target, this.options)
    28. .then(() => {
    29. console.info(`openPopup success`);
    30. })
    31. .catch((err: BusinessError) => {
    32. console.error(`openPopup error: ${err.code} ${err.message}`);
    33. });
    34. }
    35. }
    36. // 关闭popup
    37. public closePopup() {
    38. if (this.promptAction != null) {
    39. this.promptAction.closePopup(this.contentNode)
    40. .then(() => {
    41. console.info(`closePopup success`);
    42. })
    43. .catch((err: BusinessError) => {
    44. console.error(`closePopup error: ${err.code} ${err.message}`);
    45. });
    46. }
    47. }
    48. // 更新popup
    49. public updatePopup(options: PopupCommonOptions) {
    50. if (this.promptAction != null) {
    51. this.promptAction.updatePopup(this.contentNode, options, this.isPartialUpdate)
    52. .then(() => {
    53. console.info(`updatePopup success`);
    54. })
    55. .catch((err: BusinessError) => {
    56. console.error(`updatePopup error: ${err.code} ${err.message}`);
    57. });
    58. }
    59. }
    60. }
  2. 在entry包的oh-package.json5中声明har包:
    收起
    自动换行
    深色代码主题
    复制
    1. "dependencies": {
    2. "custompopupsdk": 'file:../CustomPopupSDK'
    3. },
  3. entry包使用:
    收起
    自动换行
    深色代码主题
    复制
    1. import { PromptActionClass } from 'custompopupsdk';
    2. import { ComponentContent, PromptAction } from '@kit.ArkUI';
    3. // 用于传递参数
    4. class Params {
    5. title: string = '';
    6. text: string = '';
    7. promptActionClass: PromptActionClass = new PromptActionClass();
    8. ;
    9. constructor(title: string, text: string, promptActionClass: PromptActionClass) {
    10. this.title = title;
    11. this.text = text;
    12. this.promptActionClass = promptActionClass;
    13. }
    14. }
    15. // popup内容
    16. @Builder
    17. function buildText(params: Params) {
    18. Row() {
    19. Column() {
    20. Image($rawfile('logo.png'))
    21. .width(32)
    22. .height(32)
    23. .borderRadius(8)
    24. }
    25. .width(32)
    26. .height(90)
    27. Column() {
    28. Column({ space: 2 }) {
    29. Row() {
    30. Text(params.title)
    31. .fontSize(16)
    32. .fontColor('#000000')
    33. .fontWeight(500)
    34. .lineHeight(22)
    35. .width(214)
    36. Image($rawfile('xmark.svg')).width(18).height(18)
    37. .onClick(() => {
    38. // 关闭popup
    39. params.promptActionClass.closePopup();
    40. })
    41. .margin({
    42. left: 12
    43. })
    44. }
    45. .width('100%')
    46. .height(22)
    47. Text(params.text)
    48. .fontSize(14)
    49. .fontWeight(400)
    50. .lineHeight(19)
    51. .width('100%')
    52. .maxLines(2)
    53. .textOverflow({ overflow: TextOverflow.Ellipsis })
    54. }
    55. .justifyContent(FlexAlign.Start)
    56. .width('100%')
    57. .height(62)
    58. Row() {
    59. Text('Update')
    60. .fontSize(14)
    61. .fontColor('#0A59F7')
    62. .fontWeight(500)
    63. .lineHeight(19)
    64. .onClick(() => {
    65. // 更新popup内容样式
    66. params.promptActionClass.updatePopup({
    67. enableArrow: false,
    68. });
    69. })
    70. }
    71. .width(244)
    72. .height(20)
    73. .margin({
    74. top: 8
    75. })
    76. }
    77. .margin({
    78. left: 12
    79. })
    80. .width(244)
    81. .height(90)
    82. .alignItems(HorizontalAlign.Start)
    83. .justifyContent(FlexAlign.Start)
    84. }.justifyContent(FlexAlign.Center)
    85. .width(312)
    86. .height(114)
    87. .borderRadius(20)
    88. }
    89. @Entry
    90. @Component
    91. struct Index {
    92. @State title: string = 'Title';
    93. @State text: string =
    94. 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
    95. private uiContext: UIContext = this.getUIContext();
    96. private promptAction: PromptAction = this.uiContext.getPromptAction();
    97. private promptActionClass: PromptActionClass = new PromptActionClass();
    98. private targetId: string | number = 0; // 目标组件id
    99. private contentNode: ComponentContent<Object> =
    100. new ComponentContent(this.uiContext, wrapBuilder(buildText),
    101. new Params(this.title, this.text, this.promptActionClass)); // 初始化自定义popup组件
    102. private options: PopupCommonOptions = { enableArrow: true, autoCancel: false };
    103. build() {
    104. Column() {
    105. Column() {
    106. Text('文本一')
    107. Text('给文本三加气泡')
    108. .fontColor('#0A59F7')
    109. .onClick(() => {
    110. // 指定弹出组件ID
    111. let targetId = 'column3';
    112. if (targetId == undefined) {
    113. this.targetId = 0;
    114. } else {
    115. this.targetId = targetId;
    116. }
    117. this.promptActionClass.setPromptAction(this.promptAction);
    118. this.promptActionClass.setContentNode(this.contentNode);
    119. this.promptActionClass.setOptions(this.options);
    120. this.promptActionClass.setIsPartialUpdate(false);
    121. this.promptActionClass.setTarget({ id: this.targetId });
    122. // 打开popup
    123. this.promptActionClass.openPopup();
    124. })
    125. }
    126. .width('100%')
    127. .height(100)
    128. .backgroundColor('#F1F3F5')
    129. .borderRadius(20)
    130. .id('column1')
    131. .margin({
    132. top: 10
    133. })
    134. .padding({
    135. top: 15,
    136. bottom: 15
    137. })
    138. .justifyContent(FlexAlign.SpaceEvenly)
    139. Column() {
    140. Text('文本二')
    141. Text('给文本二加气泡')
    142. .fontColor('#0A59F7')
    143. .onClick(() => {
    144. // 指定组件ID
    145. let targetId = 'column2';
    146. if (targetId == undefined) {
    147. this.targetId = 0;
    148. } else {
    149. this.targetId = targetId;
    150. }
    151. this.promptActionClass.setPromptAction(this.promptAction);
    152. this.promptActionClass.setContentNode(this.contentNode);
    153. this.promptActionClass.setOptions(this.options);
    154. this.promptActionClass.setIsPartialUpdate(false);
    155. this.promptActionClass.setTarget({ id: this.targetId });
    156. this.promptActionClass.openPopup();
    157. })
    158. }
    159. .width('100%')
    160. .height(100)
    161. .backgroundColor('#F1F3F5')
    162. .borderRadius(20)
    163. .id('column2')
    164. .margin({
    165. top: 10
    166. })
    167. .padding({
    168. top: 15,
    169. bottom: 15
    170. })
    171. .justifyContent(FlexAlign.SpaceEvenly)
    172. Column() {
    173. Text('文本三')
    174. Text('给文本一加气泡')
    175. .fontColor('#0A59F7')
    176. .onClick(() => {
    177. // 指定组件ID
    178. let targetId = 'column1';
    179. if (targetId == undefined) {
    180. this.targetId = 0;
    181. } else {
    182. this.targetId = targetId;
    183. }
    184. this.promptActionClass.setPromptAction(this.promptAction);
    185. this.promptActionClass.setContentNode(this.contentNode);
    186. this.promptActionClass.setOptions(this.options);
    187. this.promptActionClass.setIsPartialUpdate(false);
    188. this.promptActionClass.setTarget({ id: this.targetId });
    189. this.promptActionClass.openPopup();
    190. })
    191. }
    192. .width('100%')
    193. .height(100)
    194. .borderRadius(20)
    195. .backgroundColor('#F1F3F5')
    196. .id('column3')
    197. .margin({
    198. top: 10
    199. })
    200. .padding({
    201. top: 15,
    202. bottom: 15
    203. })
    204. .justifyContent(FlexAlign.SpaceEvenly)
    205. }
    206. }
    207. }

运行效果图如下: