目录

页面跳转后bindSheet不消失于原页面

问题现象

在A页面打开bindSheet后跳转其他页面,希望返回A页面时bindSheet仍是打开状态。

效果预览

背景知识

解决方案

  1. 定义状态变量isShow控制bindSheet显隐。
  2. 利用onPop回调函数的返回值PopInfo中的result的值决定isShow。在如下代码中,若传回的result的值为1,则令isShow=true。
  3. 在子页面中使用pop方法传入result的值。

完整示例参考如下:

页面一:

@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"
    }
  ]
}