文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS 自定义弹框弹出后跳转到另一个page后弹框依然存在 (API12+)

HarmonyOS 自定义弹框弹出后跳转到另一个page后弹框依然存在 (API12+)

点赞
收藏
回复
1
分享
举报
浏览50 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

可参考如下方案。

方案一:若页面存在弹窗时进行路由跳转,只会进行页面内容切换,不再自动关闭弹窗,即弹窗始终处于页面之上不会消失,具体请参考:dialog在页面路由跳转时关闭行为变更。目前解决方案就是:在路由跳转前调用弹窗的close方法进行手动关闭,或者手动开启。

方案二 :可以使用页面级弹窗,参考链接:页面级弹出框。

补充方案三,配合onPageShow()和onPageHide()方法,处理弹框弹出的逻辑。

参考代码:

收起
自动换行
深色代码主题
复制
import router from '@system.router';


@CustomDialog
struct CustomDialogExample {
  @Link isShow: boolean
  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }

  aboutToDisappear(): void {
    this.isShow = true
  }

  aboutToAppear(): void {
    this.isShow = true
  }

  build() {
    Column() {
      Text('这是自定义弹窗')
        .fontSize(30)
        .height(100)
      Button('点我关闭弹窗')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
          }
          router.push({
            // uri: 'pages/SecondPage',
            uri: 'pages/ViewB',
            params: {
              data1: 'message',
              data2: {
                data3: [123, 456, 789]
              }
            }
          });
        })
        .margin(20)
    }
  }
}

@Entry
@Component
struct TestCustomDialogDisappearPage {
  @State isShow: boolean = false //判断弹窗是否已弹出
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      isShow: this.isShow,
      cancel: () => {
        this.onCancel()
      },
      confirm: () => {
        this.onAccept()
      }
    }),
    cancel: this.existApp,
    autoCancel: true,
    onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: -20 },
    customStyle: false,
    cornerRadius: 20,
    width: 300,
    height: 200,
    borderWidth: 1,
    borderStyle: BorderStyle.Dashed, //使用borderStyle属性,需要和borderWidth属性一起使用
    borderColor: Color.Blue, //使用borderColor属性,需要和borderWidth属性一起使用
    backgroundColor: Color.White,
    shadow: ({
      radius: 20,
      color: Color.Grey,
      offsetX: 50,
      offsetY: 0
    }),
  })

  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    console.info("ah aboutToDisAppear:")
    this.dialogController = null // 将dialogController置空
  }

  onPageShow(): void {
    console.info("ah aboutToAppear:" + (this.dialogController != undefined) + "|is:" + this.isShow)
    if (this.dialogController != undefined && this.isShow) {
      this.dialogController.open()
    }
  }

  onPageHide(): void {
    console.info("ah onPageHide:")
    if (this.dialogController != undefined && this.isShow) {
      this.dialogController.close()
    }
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
      Text(`${this.isShow}`)
      Button('click me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
展开全部
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。