HarmonyOS父子组件传递参数

HarmonyOS父子组件传递参数

1. 使用@State@Prop进行父子组件传递———注意是单向同步

@Prop装饰器:父子单向同步

  • 注意:只支持单向同步,同时也只能支持string\number\boolean\enum比较简单的类型。
    在这里插入图片描述

代码

// 使用 props 进行父子组件传值
@Component
struct SonCom {
  // 父子间传递过来的数据使用 @Prop 进行接受
  @Prop sonCar: string
  // 修改传递的参数
  changeInfo = (info: string)=> {}
  build() {
    Column() {
      Text(`这是子组件的盒子--- ${this.sonCar}`)

      Button('子组件修改父组件的数据').onClick((event: ClickEvent) => {
        this.changeInfo('吉利银河 L7 ----' + Math.ceil(Math.random() * 10))
      })
    }
    .width('100%')
    .height(100)
    .backgroundColor(Color.Orange)
  }
}


@Entry
@Component
struct PropsPage {
  @State info: string = '比亚迪 宋'

  changeInfo = (newInfo: string)=>{
    this.info = newInfo
  }

  build() {
    Column({space: 20}) {
      Text(`这是父组件的盒子 ${this.info}`)

      Button('修改父组件的数据').onClick((event: ClickEvent) => {
        this.info = '领克 08---' + Math.ceil(Math.random() * 10)
      })
      // 这是子组件
      SonCom({
        sonCar: this.info,
        changeInfo: this.changeInfo
      })
    }
    .width('100%')
    .height(300)
    .backgroundColor(Color.Pink)
  }
}

演示
在这里插入图片描述

2. @Link装饰器:父子双向同步

@Link装饰器:父子双向同步

  • 注意
    在这里插入图片描述
// 子组件
@Component
struct ChildCom {
  @Link list: number[]

  build() {
    Column() {

      List({space: 10}) {
        ForEach(this.list, (item: number, index) => {
          ListItem() {
            Text(item.toString())
              .width('100%')
              .padding(10)
              .backgroundColor(Color.White)

          }
        })
      }
    }.onClick(() => {
      this.list.push(this.list.length + 1)
    })
  }
}


// 父组件
@Entry
@Component
struct StateLink {
  @State list: number[] = [1, 2, 3]

  build() {
    Column() {
      ChildCom({
        // 注意,这里调用时,使用$替换this,这是语法规定
        list: $list
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(Color.Gray)
  }
}

在这里插入图片描述

3. @Provide装饰器和@Consume装饰器:与后代组件双向同步

@Provide装饰器和@Consume装饰器:与后代组件双向同步

  • 注意@Consume装饰的变量通过相同的属性名绑定其祖先组件@Provide装饰的变量,在这里就是SunziCom中的@Consume listInfo: ListItemClass与祖先组件ProvideConsume中的@Provide listInfo: ListItemClass属性名保持一致。
// 这是模拟的数据
@Observed
class ListItemClass {
  name: string
  likeNum: number
  isLike: boolean
  comment: string

  constructor(name: string, likeNum: number, isLike: number, comment: string) {
    this.name = name
    this.likeNum = likeNum
    this.isLike = isLike === 0 ? false : true
    this.comment = comment
  }
}


// 这是 孙子组件
@Component
struct SunziCom {
  // 注意:这里的属性名要保持和 @Provide修饰的父组件属性名一致.
  @Consume listInfo: ListItemClass
  build() {
    Column() {
      Text(this.listInfo.name)
        .fontSize(18)
        .fontWeight(700)
        .fontColor('#333')
        .margin({
          bottom: 10
        })

      Row() {
        Text(this.listInfo.comment)

        Row() {
          Text(this.listInfo.likeNum.toString())
            .fontColor(this.listInfo.isLike ? Color.Red : '#333')
        }
        .onClick(() => {

          if (this.listInfo.isLike) {
            this.listInfo.likeNum -= 1
          } else {
            this.listInfo.likeNum += 1
          }
          this.listInfo.isLike = !this.listInfo.isLike
        })
      }
      .justifyContent(FlexAlign.SpaceBetween)
      .width('100%')
    }
    .padding(10)
    .borderRadius(10)
    .alignItems(HorizontalAlign.Start)
    .width('100%')
    .backgroundColor(Color.White)
  }
}
// 这是 儿子组件
@Component
struct ErziCom {
  build() {
    SunziCom()
  }
}


@Entry
@Component
struct ProvideConsume {
  @Provide listInfo: ListItemClass = new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`)
  build() {
    Column(){
      ErziCom()
    }
  }
}

在这里插入图片描述

4. 使用@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化,主要是可以处理Link遇上ForEach而导致一些奇怪的问题

@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化

// 这是模拟的数据
@Observed
class ListItemClass {
  name: string
  likeNum: number
  isLike: boolean
  comment: string

  constructor(name: string, likeNum: number, isLike: number, comment: string) {
    this.name = name
    this.likeNum = likeNum
    this.isLike = isLike === 0 ? false : true
    this.comment = comment
  }
}

function createData() {
  return [
    new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`),
    new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`),
    new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`),
    new ListItemClass(`小火车况且况且-${Math.ceil(Math.random() * 10)}`, Math.ceil(Math.random() * 100), Math.floor(Math.random() * 2), `这是随机的评论--${Math.ceil(Math.random() * 1000)}`)
  ]
}


// 子组件
@Component
struct ChildCom {
  @ObjectLink listInfo: ListItemClass

  build() {

    Column() {
      Text(this.listInfo.name)
        .fontSize(18)
        .fontWeight(700)
        .fontColor('#333')
        .margin({
          bottom: 10
        })

      Row() {
        Text(this.listInfo.comment)

        Row() {
          Text(this.listInfo.likeNum.toString())
            .fontColor(this.listInfo.isLike ? Color.Red : '#333')
        }
        .onClick(() => {

          if (this.listInfo.isLike) {
            this.listInfo.likeNum -= 1
          } else {
            this.listInfo.likeNum += 1
          }
          this.listInfo.isLike = !this.listInfo.isLike
        })
      }
      .justifyContent(FlexAlign.SpaceBetween)
      .width('100%')
    }
    .padding(10)
    .borderRadius(10)
    .alignItems(HorizontalAlign.Start)
    .width('100%')
    .backgroundColor(Color.White)
  }
}


// 父组件
@Entry
@Component
struct ObservedObjectLink {
  @State list: ListItemClass[] = createData()

  build() {
    Column() {
      List({
        space: 10
      }) {
        ForEach(this.list, (item: ListItemClass, index: number) => {
          ListItem() {
            ChildCom({
              listInfo: item
            })
          }
        })
      }
    }
    .padding(10)
    .width('100%')
    .height('100%')
    .backgroundColor(Color.Gray)
  }
}

在这里插入图片描述

### HarmonyOS 中子组件向父组件发送通知 在 HarmonyOS 开发中,为了使子组件能够有效地通知父组件并进行数据传递,通常采用事件回调的方式。这种方式允许父组件将处理逻辑作为参数传递给子组件,当特定条件满足时,子组件触发这些回调函数来通知父组件。 #### 实现方式概述 - **事件绑定**:父组件定义一个方法用于响应来自子组件的通知,并将其作为属性入子组件。 - **回调执行**:子组件内部调用接收到的方法完成对父组件的通知过程[^1]。 #### 示例代码展示 ##### 父组件部分 下面展示了如何在一个假设名为 `ParentComponent` 的类里设置监听器: ```java // 定义接口以便于解耦合 public interface OnChildEventListener { void onEventFromChild(String message); } @Component(name = "parent_component") class ParentComponent extends ComponentContainer { private final OnChildEventListener listener; public ParentComponent(Context context, OnChildEventListener listener) { super(context); this.listener = listener; } // 构建布局时初始化子组件实例并将当前对象中的listener赋过去 @Override protected void onCreate() { ChildComponent childComp = new ChildComponent(getContext(), (msg)->{ if(listener != null){ listener.onEventFromChild(msg); // 当子组件触发此lambda表达式的call back,则会调用到这里的onEventFromChild() } }); addChild(childComp); } } ``` ##### 子组件部分 这里是一个简单的 `ChildComponent` 类实现,它接收由父级提供的回调函数并在适当时候调用它们: ```java @Component(name="child_component") class ChildComponent extends ComponentContainer { private Consumer<String> eventCallback; public ChildComponent(Context context, Consumer<String> callback) { super(context); this.eventCallback = callback; } // 假设有一个按钮点击事件或者其他任何情况发生的时候我们想要通知父组件 private void someActionHappened(){ String msgToParent = "Hello from Child!"; if(eventCallback!=null){ eventCallback.accept(msgToParent); // 调用父组件注册的callback } } // ...其他业务逻辑... } ``` 上述例子说明了怎样利用Java8特性——Lambda表达式简化了父子间通信的设计模式;同时也体现了面向接口编程的思想使得程序结构更加清晰合理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值