IndexPage

在这里插入图片描述

  • 定义一个参数类
export class RouterParams {
  src:string
  constructor(str:string) {
    this.src = str
  }
}
  • 点击按钮后,将创建的RouterParams 对象传递到第二页
        Button($r('app.string.next'))
          .fontSize(16)
          .width(300)
          .height(50)
          .backgroundColor($r('app.color.button_bg'))
          .onClick(() => {
            router.pushUrl({
              url: 'pages/SecondPage',
              params: new RouterParams('Index页面传来的数据')
            }).catch((error:Error) => {
              Logger.info(TAG, 'IndexPage push error' + JSON.stringify(error));
            });
          })

SecondPage

在这里插入图片描述


import { router } from '@kit.ArkUI';
import CommonConstants from '../common/constants/CommonConstants';
import { RouterParams } from './IndexPage';

@Entry
@Component
struct SecondPage {
  @State params: RouterParams = router.getParams() as RouterParams
  @State src: string = this.params.src;

  build() {
    Row() {
      Column() {
        Text('第二页')
          .fontSize(38)
          .fontWeight(FontWeight.Bold)

        Text(this.src)
          .fontSize(16)

        Blank()
        Button($r('app.string.back'))
          .fontSize(16)
          .width(300)
          .height(50)
          .backgroundColor($r('app.color.button_bg'))
          .onClick(() => {
            router.back();
          })
      }
      .width(CommonConstants.FULL_WIDTH)
      .height(CommonConstants.LAYOUT_HEIGHT)
    }
    .height(CommonConstants.FULL_HEIGHT)
    .backgroundColor($r('app.color.page_bg'))
  }
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部