1 个回答

参考DEMO:

//Index.ets:
import router from '@ohos.router';
let msg: String = 'Index跳来的数据'

@Entry
@Component
struct routerPage {
  @State message: string = '首页';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(60)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.message = '123'
          })
        Button('跳转页面')
          .onClick(() => {
            router.pushUrl({
              url: 'pages/routerSec',
              params: {
                src: msg
              }
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

//routerSec.ets

import router from '@ohos.router';

@Entry
@Component
struct routerSec {
  @State message: string = 'Hello 123';
  @State src: string = (router.getParams() as Record<string, string>)['src'];

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(60)
          .fontWeight(FontWeight.Bold)
        Text(this.src)
          .margin({ bottom: 20 })
        Button('返回首页')
          .onClick(() => {
            router.back();
          })

      }
      .width('100%')
    }
    .height('100%')
  }
}

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-router-V5\#基于ts扩展的声明式开发范式

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进