完整示例参考如下:
页面一:
@Entry @Component struct PageOne { // 定义状态变量isShow控制bindSheet显隐 @State isShow: Boolean = false; pageInfo: NavPathStack = new NavPathStack(); @Builder myBuilder() { Column() { Button('ToSecondPage').fontSize(15).height(50).onClick(() => { this.isShow = false; this.pageInfo.pushPathByName('SecondPage', '', (onPop) => { this.isShow = (onPop.result as number) === 1; }); }); }; } build() { Navigation(this.pageInfo) { Column() { Button('bindSheet') .onClick(() => { this.isShow = true; }) .fontSize(20) .margin(10) .bindSheet($$this.isShow, this.myBuilder(), { height: SheetSize.MEDIUM, blurStyle: BlurStyle.Thick, showClose: true, title: { title: 'title', subtitle: 'subtitle' }, preferType: SheetType.CENTER, }); }; }; } }
页面二:
@Builder export function SecondPageBulider() { SecondPage(); } @Entry @Component export struct SecondPage { pageInfo = new NavPathStack(); onBackPress(): boolean | void { } build() { NavDestination() { Column() { Button('ToSecondPage').fontSize(15).height(50).onClick(() => { this.pageInfo.pop(1); }); }; } .onReady((navctx) => { this.pageInfo = navctx.pathStack; }) .width('100%') .height('100%'); } }
在“src/main”目录下的工程配置文件module.json5中的module字段里配置"routerMap": "$profile:router_map",并在“src/main/resources/base/profile”目录下新增router_map.json。
router_map.json:
{
"routerMap": [
{
"name": "SecondPage",
"pageSourceFile": "src/main/ets/pages/PageTwo.ets",
"buildFunction": "SecondPageBulider"
}
]
}