鸿蒙开发 组件之间的传值

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()

}

相关推荐
QC七哥10 天前
跨平台开发flutter初体验
android·flutter·安卓·桌面开发
HarmonyOS小助手11 天前
京东正式开源Taro on HarmonyOS C-API 版本,为鸿蒙应用跨端开发提供高性能框架
harmonyos·鸿蒙·harmonyos next·harmonyos 5.0·鸿蒙5·鸿蒙生态
在人间耕耘12 天前
鸿蒙云函数与云数据库开发实践
鸿蒙
呆dai~14 天前
基于原生能力的键盘控制
harmonyos·鸿蒙
知然18 天前
鸿蒙 Native API 的封装库 h2lib_arkbinder
c++·arkts·鸿蒙
Francek Chen22 天前
【华为Pura80系列】鸿蒙生态再升级:Pura 80 系列影像突破,WATCH 5 开启智能手表新纪元
华为·harmonyos·鸿蒙·智能手表·pura80·watch 5
编号:051723 天前
【HarmonyOS 5.0】开发实战:从UI到Native全解析
鸿蒙·devecostudio·harmonyos5.0
AirDroid_cn23 天前
打开网页即可远程控制手机,Linux系统亦可使用
linux·智能手机·安卓·远程工作·远程控制·远程控制手机·远程投屏
甜甜的资料库24 天前
基于安卓的线上考试APP源码数据库文档
安卓