在HarmonyOS NEXT开发中页面反向传值怎么传?

在HarmonyOS NEXT开发中页面反向传值怎么传?从A页面跳转到B页面,当从B页面返回时,想给A页面传递参数,如何传递?

阅读 690
1 个回答

新建三个页面分别是Index2、APage、BPage,实现了A传给B,B传给A,互传的功能。代码如下:

import { router } from '@kit.ArkUI'; 
 
@Entry 
@Component 
struct APage { 
  @State message: string = '我是A'; 
  fromPage : Record<string,string> = router.getParams() as Record<string,string> 
  @StorageLink('flag') flag : string = '' 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Text("跳转到B") 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            if (this.fromPage==null) { 
              router.pushUrl({ 
                url:"pages/BPage", 
                params:{data:"BPage"} 
              }) 
            } else{ 
              if (this.fromPage!=null) { 
                console.log("我在A页面,存入了" + this.fromPage['data'].toString()) 
                AppStorage.set('flag', this.fromPage['data'].toString()) 
                console.log("我在A页面,输出flag:" + this.flag) 
              } 
              router.pushUrl({ 
                url:"pages/BPage", 
                params:{data:"BPage"} 
              }) 
            } 
          }) 
        Text("跳转到Index2") 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            router.pushUrl({ 
              url:"pages/Index2" 
            }) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
 
 
import { router } from '@kit.ArkUI'; 
 
@Entry 
@Component 
struct BPage { 
  @State message: string = '我是B'; 
  @StorageLink('flag') flag : string = '' 
  fromPage : Record<string,string> = router.getParams() as Record<string,string> 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
        Text("跳转到A") 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            if (this.fromPage!=null) { 
              console.log("我在B页面,存入了"+this.fromPage['data'].toString()) 
              AppStorage.set('flag',this.fromPage['data'].toString()) 
              console.log("我在B页面,输出flag:"+this.flag) 
            } 
            router.pushUrl({ 
              url:"pages/APage", 
              params:{data:"APage"} 
            }) 
          }) 
        Text("跳转到Index2") 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            router.pushUrl({ 
              url:"pages/Index2" 
            }) 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
 
import { router } from '@kit.ArkUI'; 
 
@Entry 
@Component 
struct Index2 { 
  @State message: string = '当前页面'; 
 
  @StorageLink('flag') @Watch('onChange') flag : string = '' 
 
  onChange(){ 
    console.log(this.flag) 
  } 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            if (this.flag=='') { 
              console.log('flag初始化失败') 
            } else { 
              router.pushUrl({ 
                url:"pages/"+this.flag 
              }) 
            } 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进