HarmonyOS 鸿蒙Next:Component中如何接收返回的数据
HarmonyOS 鸿蒙Next:Component中如何接收返回的数据
@Entry
@Compoent
struct A{
build(){
B
}
}
@Compoent
struct B{
build(){
xxxxx
}
},
在B中使用了router跳转到了C页面,然后C页面携带参数router.back(url: xxx, params:{xxxx}),我如何能在B中获取到C带回来的params
更多关于HarmonyOS 鸿蒙Next:Component中如何接收返回的数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
请参考以下代码: 1.A.ets
import { router } from '@kit.ArkUI'
@Entry
@Component
struct A{
  @State select: boolean = true
  onPageShow(): void {
    // let aaa =  router.getParams() as Record<string, string>;
    // if (aaa) {
    //   const info: string = aaa.ttt as string; // 获取info属性的值
    //   console.info('wwwwwww1==',info)
    // }
  }
  build() {
    Column(){
      B()
    }
  }
}
@Component struct  B{
  build() {
    Column() {
      Text('B组件跳转到C')
        .onClick(() => {
          router.pushUrl({
            url: 'pages/C'
          })
        })
    }
    .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
      let aaa =  router.getParams() as Record<string, string>;
      if (aaa) {
        const info: string = aaa.ttt as string; // 获取info属性的值
        console.info('wwwwwww1==onVisibleAreaChange===',info)
      }
    })
  }
}
2.C.ets
import { router } from '@kit.ArkUI';
@Entry
@Component
struct C {
  @State message: string = 'Hello World';
  aboutToAppear(): void {
  }
  build() {
    RelativeContainer() {
      Text(this.message)
        .id('CHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(()=>{
          router.back({url:'',params:{"ttt":'返回数据'}})
        })
    }
    .height('100%')
    .width('100%')
  }
}
更多关于HarmonyOS 鸿蒙Next:Component中如何接收返回的数据的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,Component接收返回数据通常通过事件机制或回调接口实现。具体步骤如下:
- 
定义事件或回调接口:在发送数据的组件中,定义一个事件或回调接口,用于将数据传递出去。该事件或接口需要包含返回的数据类型。 
- 
注册事件监听器:在接收数据的组件中,注册一个事件监听器,用于监听来自其他组件的事件。监听器需要实现对应的事件处理函数。 
- 
触发事件:在发送数据的组件中,当数据准备好后,触发定义好的事件,并将数据作为参数传递给事件。 
- 
处理事件:接收数据的组件的事件监听器会捕获到这个事件,并在事件处理函数中获取传递过来的数据。 
- 
使用数据:在事件处理函数中,接收组件可以直接使用传递过来的数据,进行后续的逻辑处理。 
这种机制确保了组件间的数据传递是解耦的,发送组件不需要知道接收组件的具体实现,只需要触发事件即可。接收组件则通过监听事件来获取数据,实现了数据的接收。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html
 
        
       
                   
                   
                  

