HarmonyOS 页面反向传值怎么传?

从A页面跳转到B页面,当从B页面返回时,想给A页面传递参数,如何传递。

HarmonyOS
2024-11-28 09:30:50
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

新建三个页面分别是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%') 
  } 
}
  • 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.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
分享
微博
QQ
微信
回复
2024-11-28 15:35:06
相关问题
HarmonyOS 如何实现页面反向
1129浏览 • 1回复 待解决
HarmonyOS 页面
843浏览 • 1回复 待解决
HarmonyOS navigation页面之间回
828浏览 • 1回复 待解决
ArkTS如何进行页面
2428浏览 • 1回复 待解决
HarmonyOS 界面逆向
1148浏览 • 1回复 待解决
HarmonyOS Component问题
627浏览 • 2回复 待解决
HarmonyOS Checkbox如何动态
659浏览 • 1回复 待解决
HarmonyOS 父子组件问题
758浏览 • 1回复 待解决
arkts父子组件组件怎么通信啊?
6582浏览 • 1回复 待解决
HarmonyOS AKI是否支持引用
711浏览 • 1回复 待解决
HarmonyOS 页面参问题
1274浏览 • 1回复 待解决
在ArkTS中如何进行页面之间的
740浏览 • 0回复 待解决
HarmonyOS原生如何给flutter
1093浏览 • 1回复 待解决
OpenHarmony 使用WEB组件问题
4552浏览 • 1回复 待解决
HarmonyOS 页面间如何参?
1753浏览 • 1回复 待解决
HarmonyOS 调用flutter页面
639浏览 • 1回复 待解决
怎么onClick事件?
890浏览 • 1回复 待解决
HarmonyOS 页面参后对象出错
577浏览 • 1回复 待解决
自定义组件的和绑定
1691浏览 • 1回复 待解决