HarmonyOS 页面中的组件能否感知页面变化

我有个聊天页面,页面内发送文件的组件,点击该组件需要跳转到文件查看器页面,在文件查看器页面选中某个文件后,会后退回到聊天页面,此时我如何在发送文件组件获取当前文件查看器页面返回回来的参数

HarmonyOS
2025-01-09 14:35:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

请参考demo

//Index.ets
import { pageBTmp, Pages } from './pageB'

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  @Provide('pageInfo') pageInfo: NavPathStack = new NavPathStack()

  @Builder
  PageMap(name: string) {
    if (name === 'pageB') {
      pageBTmp({ names: name, values: this.pageInfo } as Pages)
    }
  }
  build() {
    Navigation(this.pageInfo) {
      Column() {
        Text(this.message)
          .width('80%')
          .height(50)
          .margin(10)

        CustomComp()
      }
    }.title('NavIndex').navDestination(this.PageMap)
  }
}

class ParamWithOp {
  operation: number = 1
  count: number = 10
}

// A页面中的自定义组件
@Component
struct CustomComp {
  @Consume pageInfo: NavPathStack
  @State routerParams: ParamWithOp | undefined = undefined

  build() {
    Column() {
      Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(10)
        .onClick(() => {
          this.pageInfo.pushPath({name: 'pageB', param: new ParamWithOp(), onPop: (popInfo: PopInfo)=>{
            // 在自定义组件中获取到返回的参数
            this.routerParams = popInfo.result as ParamWithOp
          }});
        })

      Text('返回的数据: ')
      Text(JSON.stringify(this.routerParams))
    }
  }
}

//pageB.ets
export class Pages {
  names: string = ""
  values: NavPathStack | null = null
}

class ParamWithOp {
  operation: number = 1
  count: number = 10
  constructor(count: number) {
    this.count = count;
  }
}

@Builder
export function pageBTmp(info: Pages) {
  NavDestination() {
    Column() {
      Button('pop', { stateEffect: true, type: ButtonType.Capsule })
        .width('80%')
        .height(40)
        .margin(20)
        .onClick(() => {
          // 回退到上一个页面,将处理结果传入push的onPop回调中。
          (info.values as NavPathStack).pop(new ParamWithOp(111));
        })


    }.width('100%').height('100%')
  }.title('pageB')
  .onBackPressed(() => {
    // 回退到上一个页面,将处理结果传入push的onPop回调。
    (info.values as NavPathStack).pop(new ParamWithOp(0));
    return true;
  })
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 17:30:42
相关问题
UIAbility是否可以监听页面变化
2166浏览 • 1回复 待解决
HarmonyOS 页面高度发生变化
941浏览 • 1回复 待解决
HarmonyOS能否全局监听页面的生命周期
1491浏览 • 1回复 待解决
HarmonyOS 页面组件区别
726浏览 • 1回复 待解决