HarmonyOS 如何写一个http加载中的转动弹窗,监听到http请求完成自动关闭改弹窗

HarmonyOS
2025-01-09 15:58:03
1037浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

参考示例如下:

import { ComponentContent } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Parent {
  private customDialog = new CustomDialog()
  @State message: string = ''

  build() {
    Column() {
      Text(this.message)
      Button("click").onClick(() => {
        this.customDialog.openDialog(this.getUIContext())

        //模拟请求结束关闭弹框
        setTimeout(() => {
          this.customDialog.closeDialog(this.getUIContext())
          this.message = '拿到数据'
        }, 3000)
      }).width(100).height(100)
    }.width('100%').height('100%').alignItems(HorizontalAlign.Center).justifyContent(FlexAlign.Center)
  }
}

class CustomDialog {
  openDialog(uiContext: UIContext) {
    if (uiContext) {
      let promptAction = uiContext.getPromptAction();
      let contentNode = new ComponentContent((uiContext as UIContext), wrapBuilder(buildText));
      GlobalContext.getContext().setObject('contentNode', contentNode);
      try {
        promptAction.openCustomDialog(contentNode, {
          isModal: false
        });
      } catch (error) {
        let message = (error as BusinessError).message;
        let code = (error as BusinessError).code;
        console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
      }
      ;
    }
  }

  closeDialog(uiContext: UIContext) {
    if (uiContext) {
      let promptAction = uiContext.getPromptAction();
      try {
        let contentNode = GlobalContext.getContext().getObject('contentNode') as ComponentContent<object>;
        promptAction.closeCustomDialog(contentNode);
      } catch (error) {
        let message = (error as BusinessError).message;
        let code = (error as BusinessError).code;
        console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
      };
    }
  }
}

@Builder
function buildText() {
  Column() {
    Text('加载中')
      .fontSize(24)
  }
  .backgroundColor(Color.Pink)
  .width(100)
  .height(100)
  .alignItems(HorizontalAlign.Center)
  .justifyContent(FlexAlign.Center)
}

export class GlobalContext {
  private constructor() {
  }

  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object | undefined {
    console.log(typeof (this._objects.get(value)))
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.

文档参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-uicontext-V5#opencustomdialog12

分享
微博
QQ
微信
回复
2025-01-09 19:16:58
相关问题
HarmonyOS 如何取消一个HTTP请求
678浏览 • 1回复 待解决
HarmonyOS 推荐一个http请求
833浏览 • 1回复 待解决
HarmonyOS 如何写一个工具获取User-Agent
1004浏览 • 1回复 待解决
如何实现一个带动画弹窗
1301浏览 • 1回复 待解决
ArkTS如何实现一个底部弹窗
1807浏览 • 1回复 待解决
HTTP请求使用同SESSIONID
1355浏览 • 1回复 待解决
HarmonyOS http请求封装
949浏览 • 1回复 待解决
http 请求直报 2300058
2556浏览 • 0回复 待解决