HarmonyOS router.back返回参数如何接收

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu
import router from '@ohos.router'
import { BusinessError } from '@ohos.base'

@Entry
@Component
struct Page6181 {
  @State str: string = ''
  onPageShow(): void {
    const params = (router.getParams() as Record<string, string>);
    if (params) {
      const info: string = params.info as string; // 获取info属性的值
      this.str = info
    }

  }

  async routePage() {
    let options:router.RouterOptions = {
      url: 'pages/Page6182',
      params: {
        info:"第一页的值"
      }
    }
    try {
      await router.pushUrl(options)
    } catch (err) {
      console.info(` fail callback, code: ${(err as BusinessError).code}, msg: ${(err as BusinessError).message}`)
    }
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text('这是第一页')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
      Button() {
        Text('next page')
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
      }.type(ButtonType.Capsule)
      .margin({ top: 20 })
      .backgroundColor('#ccc')
      .onClick(() => {
        this.routePage()
      })
      Row(){
        Text(`这是第二页传过来的值:${this.str}`)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

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


Page6182
import router from '@ohos.router'
import { BusinessError } from '@kit.BasicServicesKit'

@Entry
@Component
struct Page6182 {

  async routePage() {
    let options:router.RouterOptions = {
      url: 'pages/Page6181',
      params: {
        info:"第二页的值"
      }

    }
    try {
      await router.pushUrl(options)
    } catch (err) {
      console.info(` fail callback, code: ${(err as BusinessError).code}, msg: ${(err as BusinessError).message}`)
    }
  }
  onPageShow(): void {
    this.secondData = this.text
  }

  private content: string = "这是第二页"
  @State text: string = (router.getParams() as Record<string, string>).info
  @State secondData: string = ''

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text(`${this.content}`)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
      Text(`第一页传来的数值:${this.secondData}`)
        .fontSize(20)
        .margin({ top: 20 })
        .backgroundColor('red')

      Button("携带参数返回")
        .onClick(() => {
          this.routePage()
        })
    }
    .width('100%')
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
返回页面router.back如何传递参数
999浏览 • 2回复 待解决
HarmonyOS router.back返回页面问题
49浏览 • 1回复 待解决
router.back如何携带参数返回给上一级
1594浏览 • 1回复 待解决
如何获取router.back传递的参数
2710浏览 • 1回复 待解决
HarmonyOS 关于router.back()
733浏览 • 1回复 待解决
HarmonyOS router.back无法返回到上一页
29浏览 • 1回复 待解决
HarmonyOS router.back不起作用
130浏览 • 1回复 待解决
router.back中加入指定url后无法返回
338浏览 • 1回复 待解决
能否拦截左滑返回并替换为router.back
736浏览 • 1回复 待解决
HarmonyOS 关于router.back问题的咨询
460浏览 • 1回复 待解决
router.back调用时,偶现app自动退出
1754浏览 • 1回复 待解决
HarmonyOS 路由参数如何接收
416浏览 • 1回复 待解决
HarmonyOS Router如何返回指定页面
40浏览 • 1回复 待解决
HarmonyOS 路由跳转怎么接收参数
44浏览 • 1回复 待解决