HarmonyOS 关于openCustomDialog方法

import { promptAction } from '@kit.ArkUI'
let customDialogId: number = 0
@Builder
export function buildText() {
    Column() {
        Text('Custom dialog Message').fontSize(10)
        Row() {
            Button("确认").onClick(() => {
                promptAction.closeCustomDialog(customDialogId)
            })
            Blank().width(50)
            Button("取消").onClick(() => {
                promptAction.closeCustomDialog(customDialogId)
            })
        }
    }.height(200).padding(5)
} //自定义组件的内容


axiosClient.interceptors.response.use(
    async (response: AxiosResponse) => {
    let windowClass = await window.getLastWindow(getContext())
    let uiContext = windowClass.getUIContext()
    let promptAction = uiContext.getPromptAction();
    let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), 'data.message');
    promptAction.openCustomDialog(contentNode, {
        onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
        console.info("reason" + JSON.stringify(dismissDialogAction.reason))
        console.log("dialog onWillDismiss")
    }
})
}
  • 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.

我在拦截器中做测试 无法弹出这个弹窗,这是因为什么原因呢

HarmonyOS
2024-12-25 11:52:49
340浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

需要手动获取UIContext,示例如下:

1、创建一个全局Context,GlobalContext.etx:

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 {
    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.

2、在【EntryAbility.ets --> onWindowStageCreate】中,获取主窗口:

GlobalContext.getContext().setObject("windowStage", windowStage);
  • 1.

3、调用:

let windowStage = GlobalContext.getContext().getObject("windowStage") as window.WindowStage
windowStage.getMainWindowSync().getUIContext().getPromptAction().showToast({
  message:'请求失败'
});
  • 1.
  • 2.
  • 3.
  • 4.
分享
微博
QQ
微信
回复
2024-12-25 15:03:54


相关问题
HarmonyOS openCustomDialog问题
290浏览 • 1回复 待解决
HarmonyOS 关于AOP具体使用方法
1009浏览 • 1回复 待解决
HarmonyOS promptAction.openCustomDialog问题
350浏览 • 1回复 待解决
promptAction.openCustomDialog 全局弹窗
1039浏览 • 1回复 待解决
关于私钥SHA256加签方法
678浏览 • 1回复 待解决
promptAction.openCustomDialog 自定义弹窗
674浏览 • 1回复 待解决