鸿蒙开发 组件之间的传值

1.@Prop:父组件传递给子组件,单向传递,子组件改变值,父组件UI不更新。

引入子组件 并赋值,子组件用@Prop 接收

import headerView from '../../common/bean/BaseNavHeaderView'

复制代码
headerView({
  titlestr:'添加地址',
  isback:true,
  isleftimage:'',
  isright:true,
  rightcontentstr:'保存',
  rightimage:'',
  fromehere:'添加地址',
  startCalcute:this.startCalcuteMoney.bind(this)
}).width('100%').height('44').backgroundColor('#FFD615').zIndex(888)
复制代码
@Component
export default  struct BaseNavHeaderView {

  @Prop titlestr:string;
        isback:Boolean;
        isleftimage:string;
        isright:Boolean;
        rightcontentstr:string;
       rightimage:string;
       fromehere:string;
      startCalcute:() => void = () =>{}

2.@Link 双向传递,子组件改变父组件UI更新,注意:不能修饰带@Entry 类型的pages.

父试图定义变量,传递给子组件child时要用$

复制代码
@State dianjicounts:number = 0

child({dianjicounts:$dianjicounts})

子组件接收

@Component export default struct child {

@Link dianjicounts:number

调用用this.dianjicounts

}

3.@Provide/@Consume 不需要传递参数,UI会更新

父组件定义并调用子组件

@Provide dianjicounts: number = 0

child()

子组件接收

@Component export default struct child {

@Consume dianjicounts:number

调用用this.dianjicounts

}

4.方法传递

父组件定义方法

startCalcuteMoney():void{

console.log('保存一下')

}

传给子组件,要用bind方法绑定一下

headerView({

startCalcute:this.startCalcuteMoney.bind(this)

})

子组件接收

@Component export default struct BaseNavHeaderView {

@Prop startCalcute:() => void = () =>{}

子组件调用

this.startCalcute()

}

相关推荐
OH_TPC9 小时前
HarmonyOS APP开发---“启动秀“应用引导页App,需要用到这个库
华为·harmonyos·鸿蒙
2501_9197490317 小时前
华为鸿蒙免费戒咖啡APP—小羊戒咖
华为·harmonyos·鸿蒙
2501_9197490320 小时前
华为鸿蒙免费戒烟APP—小羊戒烟
华为·harmonyos·鸿蒙
math_hongfan21 小时前
鸿蒙ArkTS手势交互:拖拽、缩放、旋转与组合手势
学习·华为·交互·harmonyos·鸿蒙
超爱西西鸭1 天前
鸿蒙ArkTS文件管理:fileIo 文件读写与目录操作
学习·华为·harmonyos·鸿蒙
超爱西西鸭1 天前
ArkTS传感器开发:加速度计与数据监听
学习·华为·harmonyos·鸿蒙
math_hongfan1 天前
鸿蒙 ArkTS 国际化:多语言支持与资源管理
学习·华为·harmonyos·鸿蒙
超爱西西鸭1 天前
基于HarmonyOS的表单与校验:输入验证与正则表达式
学习·华为·harmonyos·鸿蒙
2501_919749032 天前
华为鸿蒙记录怀孕APP—小羊怀孕
华为·harmonyos·鸿蒙
超爱西西鸭2 天前
ArkTS延安精神学习应用:四版块 + 三组展开状态 + 大事记时间轴
学习·华为·harmonyos·鸿蒙