需要在module.json5配置文件的module标签中定义routerMap标签,指向定义的路由表配置文件,例如:"routerMap": "$profile:router_map"。
使用PromptAction弹窗,监听到系统侧滑事件后,若当前弹窗禁止侧滑消失,如何让事件响应到其他弹窗或路由页面上?
有两种方式实现侧滑关闭操作透传到其他页面。
import { PromptAction } from '@kit.ArkUI';
@Entry
@Component
struct Example1 {
private customDialogComponentId: number = 0;
private customDialogComponentIdTWO: number = 0;
private promptAction: PromptAction = this.getUIContext().getPromptAction();
@Builder
customDialogComponent() {
Column() {
Text('弹窗1').fontSize(30);
Row({ space: 50 }) {
Button('确认').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentId);
})
Button('取消').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentId);
})
}.padding({ bottom: 16 })
Button('打开第二弹窗')
.onClick(() => {
this.promptAction.openCustomDialog({
builder: () => {
this.customDialogComponentTWO();
},
onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
if (dismissDialogAction.reason === DismissReason.PRESS_BACK) {
this.promptAction.closeCustomDialog(this.customDialogComponentId);
}
},
autoCancel: false
}).then((dialogId: number) => {
this.customDialogComponentIdTWO = dialogId;
});
})
}.height(200).padding(16).justifyContent(FlexAlign.SpaceBetween)
}
@Builder
customDialogComponentTWO() {
Column() {
Text('弹窗2').fontSize(30)
Row({ space: 50 }) {
Button('确认').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentIdTWO);
})
Button('取消').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentIdTWO);
})
}
}.height(200).padding(16).justifyContent(FlexAlign.SpaceBetween)
}
build() {
Row() {
Column({ space: 20 }) {
Text('应用弹窗')
.fontSize(30)
.onClick(() => {
this.promptAction.openCustomDialog({
builder: () => {
this.customDialogComponent();
},
onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
if (dismissDialogAction.reason === DismissReason.PRESS_BACK) {
dismissDialogAction.dismiss();
}
},
autoCancel: false
}).then((dialogId: number) => {
this.customDialogComponentId = dialogId;
});
})
}
.width('100%')
}
.height('100%')
}
} Navigation页面,创建弹窗时,在onWillDismiss里,监听侧滑事件,当"dismissDialogAction.reason === DismissReason.PRESS_BACK"时,使用getAllPathName()获取栈中所有NavDestination页面的名称,通过popToName实现指定返回。
@Entry
@Component
struct Example2 {
@Provide('pageStack') pageStack: NavPathStack = new NavPathStack();
build() {
Navigation(this.pageStack) {
Text('跳转Page1')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.backgroundColor("#87ceeb")
.onClick(() => {
this.pageStack.pushPathByName('Page1', null, false);
})
}
.height('100%')
.width('100%')
}
} @Component
export struct Page1 {
@Consume('pageStack') pageStack: NavPathStack;
build() {
NavDestination() {
Text('跳转page2')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.backgroundColor("#87ceeb")
.onClick(() => {
this.pageStack.pushPathByName('Page2', null, false);
})
}
.width('100%')
.height('100%')
}
}
@Builder
export function getPage1RouterMap(): void {
Page1();
} import { PromptAction } from '@kit.ArkUI';
@Component
export struct Page2 {
@Consume('pageStack') pageStack: NavPathStack;
private customDialogComponentIdTWO: number = 0;
private promptAction: PromptAction = this.getUIContext().getPromptAction();
@Builder
customDialogComponentTWO() {
Column() {
Text('弹窗').fontSize(30)
Row({ space: 50 }) {
Button('确认').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentIdTWO);
})
Button('取消').onClick(() => {
this.promptAction.closeCustomDialog(this.customDialogComponentIdTWO);
})
}
}.height(200).padding(16).justifyContent(FlexAlign.SpaceBetween)
}
build() {
NavDestination() {
Text('打开弹窗')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.backgroundColor('#87ceeb')
.onClick(() => {
this.promptAction.openCustomDialog({
builder: () => {
this.customDialogComponentTWO();
},
onWillDismiss: (dismissDialogAction: DismissDialogAction) => {
if (dismissDialogAction.reason === DismissReason.PRESS_BACK) {
let pages: string[] = this.pageStack.getAllPathName();
if (pages.length > 1) {
this.pageStack.popToName(pages[0], false);
} else {
this.pageStack.clear();
}
}
}
}).then((dialogId: number) => {
this.customDialogComponentIdTWO = dialogId;
});
})
}
.width('100%')
.height('100%')
}
}
@Builder
export function getPage2RouterMap(): void {
Page2();
} 需要在module.json5配置文件的module标签中定义routerMap标签,指向定义的路由表配置文件,例如:"routerMap": "$profile:router_map"。
{
"routerMap": [
{
"name": "Page1",
"pageSourceFile": "src/main/ets/pages/Page1.ets",
"buildFunction": "getPage1RouterMap"
},
{
"name": "Page2",
"pageSourceFile": "src/main/ets/pages/Page2.ets",
"buildFunction": "getPage2RouterMap"
}
]
}