HarmonyOS开发者版主1|
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 未知归属地
写回答
所见即所得编辑器, editor1
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
相关推荐
换一批
1
HarmonyOS 弹框适配分栏模式 (API12+)
2
HarmonyOS for循环后怎么让长按手势只控制单个控件弹框 (API12+)
3
HarmonyOS 如何实现弹框的选项经过倒计时后显示(API12+)
4
HarmonyOS openCustomDialog弹框展示时是否有方法可以监听到 (API12+)
5
HarmonyOS 页面跳转但是自定义弹窗没有销毁 (API12+)
6
HarmonyOS 如何实现跳转新页面,再返回原页面,原页面弹窗仍是拉起状态?(API12+)
7
HarmonyOS A页面弹窗,点击弹窗跳转B页面,弹窗不消失 (API12+)
8
HarmonyOS 弹窗页面以及从弹窗跳转其余页面无动画(API12+)
了解社区公约,与您携手共创和谐专业的开发者社区。




































苏公安网备 32011402010933号