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

HarmonyOS
2024-12-20 16:22:33
622浏览
收藏 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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
分享
微博
QQ
微信
回复
2024-12-20 19:12:46
相关问题
返回页面router.back如何传递参数
1632浏览 • 2回复 待解决
HarmonyOS router.back返回页面问题
840浏览 • 1回复 待解决
router.back如何携带参数返回给上一级
2452浏览 • 1回复 待解决
如何获取router.back传递的参数
3604浏览 • 1回复 待解决
HarmonyOS router.back()如何获取返回
1212浏览 • 1回复 待解决
HarmonyOS Vue页面router.back()无法返回
748浏览 • 1回复 待解决
HarmonyOS 关于router.back()
1434浏览 • 1回复 待解决
HarmonyOS router.back无法返回到上一页
644浏览 • 1回复 待解决
能否拦截左滑返回并替换为router.back
1230浏览 • 1回复 待解决
HarmonyOS router.back不起作用
672浏览 • 1回复 待解决
router.back中加入指定url后无法返回
942浏览 • 1回复 待解决
HarmonyOS router.back无法退出应用
773浏览 • 1回复 待解决
HarmonyOS 关于router.back问题的咨询
1201浏览 • 1回复 待解决
router.back调用时,偶现app自动退出
2339浏览 • 1回复 待解决